Script for Random Number

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Script for Random Number

Post by gordonshim » Sun Aug 11, 2013 7:56 pm

Not being a "real" programmer, I'm a bit intimated by code written in this forum and others that I've searched for online. I am looking for a simple script that involves picking a number in an array of just 9 numbers (but no more than 100) only ONCE and continue picking until all numbers 9 numbers (or all 100) are picked. I sure someone out there has this "canned." Thank you in advance.

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Thu Jun 29, 2006 4:16 pm

Re: Script for Random Number

Post by Randy Hengst » Sun Aug 11, 2013 8:47 pm

I'm not totally sure what you're looking for… put this in a button and see if it's at all along the lines you were thinking.

on mouseUp
local tYourNumberList
put empty into tYourNumberList

repeat with x = 1 to 100 -- or to whatever number you want
put x & "," after tYourNumberList
end repeat
delete char -1 tYourNumberList -- removes the trailing comma-- which you need to do before a sort

sort items of tYourNumberList by random(10000)
put tYourNumberList -- just to see the contents
end mouseUp

gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Re: Script for Random Number

Post by gordonshim » Mon Aug 12, 2013 4:43 am

Randy...Thank you for this script. I placed it into a button of a stack I am creating for my class. Your script works well, but not for what I had in mind. Let me clarify myself. I want to randomly select multipliers 1 to 9 before I use it to multiply it with other values. Once I use a multiplier, I need it to be removed so that it won't be used again. Succeeding random multipliers should also be removed until all 9 values (multipliers) are used. Yes, I want my students to practice their times tables for multiplier values 1 to 9 only (forget about the 10, 11, and 12 that I had to memorize when I was in the 4th grade!!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Script for Random Number

Post by Simon » Mon Aug 12, 2013 5:28 am

Hi gordonshim,
Not sure if I got this right either but:
New stack with 1 fld and 1 button on it:
enter 1,2,3,4,5,6,7,8,9 into the field and

Code: Select all

on mouseUp
   get random(number of items of fld 1)
-- do something with the it var.
   delete item it of fld 1
   if fld 1 = "" then disable me
end mouseUp
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Script for Random Number

Post by Dixie » Mon Aug 12, 2013 5:29 am

Gordon...

Does this help ?

Code: Select all

local theMultipliers, theNumber

on resetTheMultipliers
   repeat with count = 1 to 9
      put count & comma after theMultipliers
   end repeat
   delete the last char of theMultipliers
end resetTheMultipliers

on mouseUp
   if theMultipliers is empty then
      resetTheMultipliers
   end if
   
   /* have theNumber as a local variable, so that you can use it later how you wish */
   put random(the number of items of theMultipliers) into theNumber
   /* get rid of the last chosen multiplier from the list of multipliers
   so that you don't use it again*/
   delete item theNumber of theMultipliers
   /* so you can see the list of multipliers that is left */
   put theMultipliers
end mouseUp

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Script for Random Number

Post by Simon » Mon Aug 12, 2013 5:42 am

Gordon,
Whats really good is if you see Dixie's script and mine are the same.
Dixie's is better because he has more 'filthy lucre' :) :) :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Re: Script for Random Number

Post by gordonshim » Mon Aug 12, 2013 5:46 am

Hello Dixie.....WOW!!..Works great!! I can't thank you enough. Should be able to use this script in other areas of future random number selection.

Hello again...Yes it does work, but I can't seem to capture that number that is deleted from what is left. I created this fld that I named "theLeftOverX" to see what is left. It does show what's left after pressing this button the first time. I need to capture all deleted multiplier when this button is activated so I created this fld that I named "theActiveX" to see. The first deleted multiplier does show up into this fld. However, subsequent deleted multipliers still shows up into my "theLeftOverX" Please advise.
Last edited by gordonshim on Mon Aug 12, 2013 6:35 pm, edited 1 time in total.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Script for Random Number

Post by Klaus » Mon Aug 12, 2013 1:14 pm

HI all,

will move this thread to the correct forum, since this is NOT a feature request in the actual meaning of the term in this context 8-)

gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Re: Script for Random Number

Post by gordonshim » Mon Aug 12, 2013 6:25 pm

Hi Klaus,
So where did you move it to?...tanks!

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Script for Random Number

Post by Klaus » Mon Aug 12, 2013 6:38 pm

Moved it to here! :-D

Come on, Gordon, check the top of this page and you will see... 8-)

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Script for Random Number

Post by Simon » Mon Aug 12, 2013 6:47 pm

Well I feel slighted.
Last time I lift up my skirt for Gordon. :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Script for Random Number

Post by Dixie » Mon Aug 12, 2013 6:57 pm

Gordon...

I think this will do what you want
Attachments
multipliers.livecode.zip
(1.21 KiB) Downloaded 283 times

gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Re: Script for Random Number

Post by gordonshim » Mon Aug 12, 2013 7:50 pm

U DA BEST!! .... must do this for a living!! I'm now able to capture the last value in the used fld and I'm now on my way to completing this stack before school starts. Many thanks for hanging with me. I really do appreciate that. My apologies to you Klaus and Simon for being such a rookie at this. I am definitely learning thanks to all that contribute to this forum. Maybe someday, I will return the favor. Now...back to my day job!!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Script for Random Number

Post by [-hh] » Sun Sep 08, 2013 1:10 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:17 pm, edited 1 time in total.
shiftLock happens

gordonshim
Posts: 9
Joined: Sun Jun 30, 2013 8:23 pm

Re: Script for Random Number

Post by gordonshim » Sun Sep 08, 2013 3:42 pm

Yes you are correct. Random "sort" is accurate. I get it. Thank you.

Post Reply

Return to “Talking LiveCode”