Page 1 of 1
Random numbers.
Posted: Sat Feb 12, 2011 7:03 pm
by dburdan
I am trying to generate 4 random number between 1 & 52 without generating the same one twice. Here is my current code:
Code: Select all
function randomNum lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomNum
repeat with i = 1 to 4
put randomNum(1,52) into y
if y > 9 then
put "50" & y into x
else
put "500" & y into x
end if
if i = 1 then
put x into vCard1
end if
if i = 2 then
put x into vCard2
end if
if i = 3 then
put x into vCard3
end if
if i = 4 then
put x into vCard4
end if
end repeat
I can't figure out how to generate the 4 numbers without generating the same one more than once.
Re: Random numbers.
Posted: Sat Feb 12, 2011 7:49 pm
by Randy Hengst
You can go a different route....
on mouseUp
local tTemp
repeat with x = 1 to 52
put x & comma after tTemp
end repeat
delete last char of tTemp -- removes trailing comma
sort items of tTemp by random(10000)
-- now take the first four numbers in tTemp
end mouseUp
Re: Random numbers.
Posted: Sat Feb 12, 2011 7:57 pm
by bn
Hi dburdan,
you could also try this, it should work, no guarantees though.
Code: Select all
put "" into tTestDupes
repeat with i = 1 to 4
put randomNum(1,52) into y
repeat while y is among the items of tTestDupes
put randomNum(1,52) into y
end repeat
put y into item (the number of items of tTestDupes +1) of tTestDupes
if y > 9 then
put "50" & y into x
else
put "500" & y into x
end if
if i = 1 then
put x into vCard1
end if
if i = 2 then
put x into vCard2
end if
if i = 3 then
put x into vCard3
end if
if i = 4 then
put x into vCard4
end if
end repeat
Just remember that random is pseudo random. But it should suffice. If you want to get a license for a casino though...

Have a look at randomseed in the dictionary.
Kind regards
Bernd
Re: Random numbers.
Posted: Sat Feb 12, 2011 8:26 pm
by dburdan
Thanks once again Bernd! That solution seems to have worked so far! As for random, I put the milliseconds into random seed. That seems to keep it pretty random.
Re: Random numbers.
Posted: Tue Mar 01, 2011 10:22 pm
by neo42
You could create an array of all numbers between 1 and 52.
Then loop through the list and randomly swap each value with another value in the list.
Then pull the first 4 numbers from the list. This would work especially well if you wanted to keep pulling from this list.
Re: Random numbers.
Posted: Tue Mar 01, 2011 11:44 pm
by BvG
I have had a similar problem, and simply created a pre-made list, then use the sort command with random(52):
Code: Select all
put the savedCardList of me into theCards
sort theCards by random(52)
where the list would be:
...
Spade 2
Spade 3
Spade 4
Spade 5
Spade 6
...
Diamond 10
Diamond J
Diamond Q
Diamond K
Diamond A
...
Re: Random numbers.
Posted: Wed Mar 02, 2011 11:46 pm
by jsburnett
But your codes don't prevent the same number from being selected twice
put "1,2,3,4,5,6,....52" into randomNumbers
repeat with x = 1 to 4
put random(the number of items of randomNumbers) into randomPick
put item randomPick of randomNumbers into item x of randomList
delete item randomPick of randomNumbers
end repeat
Re: Random numbers.
Posted: Thu Mar 03, 2011 12:06 am
by jsburnett
Wow,
I actually looked at BvG's post closer.
I never consideredt his solution.
I tried it and it works great.
Generate a list of 52 'things' on 52 lines in a field "Sort Field" - ie,
1
2
3
4
5
....
52
then... in a button put ...
on mouseUp
sort field "Field Sort" by random(52)
end mouseUp
Works great....
then you can take the top 4 lines ...... to get your 4 random numbers (or pseudorandom numbers)
JB