LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
ukaussie
- Posts: 23
- Joined: Mon Sep 28, 2009 9:04 pm
Post
by ukaussie » Fri Oct 02, 2009 12:00 am
Hi,
I've made a 'lotto' random number generator, but i'm getting quite a few instances of the same number being generated in any given 6 number sequence. How can i code the script to say "if you've generated this number before then generate another random number"?
My code for the button to start the process is:
Code: Select all
on mouseUp
put empty into field "Number 1"
put empty into field "Number 2"
put empty into field "Number 3"
put empty into field "Number 4"
put empty into field "Number 5"
put empty into field "Number 6"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 1"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 2"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 3"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 4"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 5"
wait 2 seconds
get the random of 49
put it into field "Random Number"
put it into field "Number 6"
end mouseUp
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Fri Oct 02, 2009 2:14 am
If you really want random, then eliminating numbers that have already come up is the wrong way to do it. Since your pool is only 49 numbers, by the time you've gone through 48 "random" numbers, I can tell you what the next one will be...
I'd go about this a different way: use a larger pool for the random numbers, then whittle it down to the appropriate size. Something like
Code: Select all
put random(5000) into x
put x mod 49 into field "Random Number"
-
ukaussie
- Posts: 23
- Joined: Mon Sep 28, 2009 9:04 pm
Post
by ukaussie » Fri Oct 02, 2009 12:53 pm
hi, and thanks for your response. I tried your suggestion but it didn't seem to work. I still get sequences like 6, 14, 22, 6, 39, 42.
However, maybe i didn't make myself clear...In a lottery draw, 6 numbers are drawn out of a barrel. If the first number is 10, then the second, third, fourth, fifth and sixth numbers pulled out of the barrel can't be a 10. This is what i want to script...if "Number 1" is 10, then give me a different random number to put into "Number 2" etc. Once i have all 6 numbers the process is finished. If i hit the button again, then i don't care if another number 10 is drawn, because it's a different set of 6. I thought the 'if/then/else' statement would be able to do this?
-
gyroscope
- Livecode Opensource Backer

- Posts: 404
- Joined: Tue Jan 08, 2008 3:44 pm
-
Contact:
Post
by gyroscope » Fri Oct 02, 2009 2:23 pm
Hi ukaussie
The following code works as you need, although there might be a more straightforward way to go:
Code: Select all
on mouseUp
put empty into field "Random Number"
put empty into field "Number 1"
put empty into field "Number 2"
put empty into field "Number 3"
put empty into field "Number 4"
put empty into field "Number 5"
put empty into field "Number 6"
put 0 into tOne
put 0 into tTwo
put 0 into tThree
put 0 into tFour
put 0 into tFive
put 0 into tSix
wait 2 seconds
------number 1
put the random of 49 into tOne
put tOne into field "Random Number"
put tOne into field "Number 1"
wait 2 seconds
----number 2
put the random of 49 into tTwo
if tTwo <>tOne then
put tTwo into field "Random Number"
put tTwo into field "Number 2"
else
repeat until tTwo <> tOne
put the random of 49 into tTwo
end repeat
put tTwo into field "Random Number"
put tTwo into field "Number 2"
end if
wait 2 seconds
----number 3
put the random of 49 into tThree
if tThree<>tOne and tThree <>tTwo then
put tThree into field "Random Number"
put tThree into field "Number 3"
else
repeat until tThree <> tOne and tThree <>tTwo
put the random of 49 into tThree
end repeat
put tThree into field "Random Number"
put tThree into field "Number 3"
end if
wait 2 seconds
-----number 4
put the random of 49 into tFour
if tFour<>tOne and tFour<>tTwo and tFour<>tThree then
put tFour into field "Random Number"
put tFour into field "Number 4"
else
repeat until tFour<>tOne and tFour<>tTwo and tFour<>tThree
put the random of 49 into tFour
end repeat
put tFour into field "Random Number"
put tFour into field "Number 4"
end if
wait 2 seconds
-----number 5
put the random of 49 into tFive
if tFive<>tOne and tFive<>tTwo and tFive<>tThree and tFive<>tFour then
put tFive into field "Random Number"
put tFive into field "Number 5"
else
repeat until tFive<>tOne and tFive<>tTwo and tFive<>tThree and tFive<>tFour
put the random of 49 into tFive
end repeat
put tFive into field "Random Number"
put tFive into field "Number 5"
end if
wait 2 seconds
------number 6
put the random of 49 into tSix
if tSix<>tOne and tSix<>tTwo and tFSix<>tThree and tSix<>tFour and tSix<>tFive then
put tSix into field "Random Number"
put tSix into field "Number 6"
else
repeat until tSix<>tOne and tSix<>tTwo and tSix<>tThree and tSix<>tFour and tSix<>tFive
put the random of 49 into tSix
end repeat
put tSix into field "Random Number"
put tSix into field "Number 6"
end if
end mouseUp
Hope that's helpful.

Last edited by
gyroscope on Fri Oct 02, 2009 3:49 pm, edited 1 time in total.
-
gmccarthy
- Posts: 62
- Joined: Sat Feb 17, 2007 4:56 am
Post
by gmccarthy » Fri Oct 02, 2009 3:11 pm
How about this:
Code: Select all
on mouseup
put item 1 to 6 of fGetSortedItemsRandom(fGetNumberString(1,1,49))
end mouseup
function fGetSortedItemsRandom pItemList
put the number of items of pItemList into tmaxnum
sort items of pItemList by random(tmaxnum)
return pItemList
end fGetSortedItemsRandom
function fGetNumberString pStart,pInc,pNum
put empty into tString
put pStart into tString
repeat pNum -1
add pInc to pStart
put comma & pStart after tString
end repeat
return tString
end fGetNumberString
This will make sure that you can't get a repeated number in the six.
gmcrev
-
Klaus
- Posts: 14177
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Fri Oct 02, 2009 3:15 pm
Hi friends,
why not fill a and draw from a "real virtual" pool?
Less typing guaranteed
Code: Select all
...
## Fill the virtual pool with 49 numbers
repeat with i = 1 to 49
put i & CR after mypool
end repeat
## Get rid of trailing CR
delete char -1 of mypool
## Mix pool heavily :-)
sort lines of mypool by random(10000)
## "Draw" six random and different numbers
put line 1 of mypool into rnr1
put line 2 of mypool into rnr2
put line 3 of mypool into rnr3
put line 4 of mypool into rnr4
put line 5 of mypool into rnr5
put line 6 of mypool into rnr6
## The numbers in the variables rnr1 to rnr6 are now ready to use...
...
Best
Klaus
-
gmccarthy
- Posts: 62
- Joined: Sat Feb 17, 2007 4:56 am
Post
by gmccarthy » Fri Oct 02, 2009 3:32 pm
Klaus,
Basically that's what my code in the earlier post does.
Do you know whether using a larger number in the random function makes any difference?
gmcrev
-
BvG
- VIP Livecode Opensource Backer

- Posts: 1239
- Joined: Sat Apr 08, 2006 1:10 pm
-
Contact:
Post
by BvG » Fri Oct 02, 2009 4:22 pm
actually, Klaus does something different. However, he's using a method that's not exactly intuitive to understand. This is the same method that Klaus uses, but a bit differently coded:
Code: Select all
on mouseUp
-- create all possible numbers in a list
repeat with theNumber = 1 to 49
put theNumber & return after theListOfNumbers
end repeat
--there's an empty line at the bottom, so delete last return char
delete char -1 of theListOfNumbers
--most important step: jumble the list randomly
--no need to use an arbitrary number, this approach will always fit the size of the list
sort theListOfNumbers by random(the number of lines in theListOfNumbers)
--get six randomly drawn numbers:
repeat with theNumber = 1 to 6
put line 1 of theListOfNumbers into field ("number" && theNumber)
put line 1 of theListOfNumbers into field "random number"
--and now get rid of the number, so it isn't drawn again
delete line 1 of theListOfNumbers
end repeat
of course now you need to make sure that your list of numbers is stored somewhere, but where depends on your own code etc.
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
-
Klaus
- Posts: 14177
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Fri Oct 02, 2009 5:51 pm
Hi gmccarthy,
sorry, had not the time to read all the posts here, that's why I posted my script.
I think the larger the number the more random the result

No, to be honest I have no idea
Best
Klaus
-
Klaus
- Posts: 14177
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Fri Oct 02, 2009 5:54 pm
BvG wrote:actually, Klaus does something different. However, he's using a method that's not exactly intuitive to understand. ...
I think we should let ukaussie decide

-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Fri Oct 02, 2009 7:32 pm
Here's a slightly different approach:
Code: Select all
on mouseUp
local tNumber
local tListOfNumbers
repeat with tNumber = 1 to 49
put tNumber & return after tListOfNumbers
end repeat
--there's an empty line at the bottom, so delete last return char
delete char -1 of tListOfNumbers
repeat with x=1 to 6
put any line of tListOfNumbers into field x
delete line x of tListOfNumbers
end repeat
end mouseUp
-
ukaussie
- Posts: 23
- Joined: Mon Sep 28, 2009 9:04 pm
Post
by ukaussie » Fri Oct 02, 2009 11:55 pm
Hi everyone. Thanks for all your responses! In the end I've used the one suggested by BvG because a) it works, and b) as a beginner i could understand it! (sorry Klaus). Anyway, I really appreciate all your help...I'm learning a lot.
Cheers.
-
Mark Smith
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
-
Contact:
Post
by Mark Smith » Sat Oct 03, 2009 12:25 pm
Here's a shorter (though not necessarily more efficient) way:
on mouseUp
put empty into tArray
repeat until the number of lines in keys(tArray) = 6
put 0 into tArray[random(49)]
end repeat
put keys(tArray)
end mouseUp
Best,
Mark