Getting into LiveCode for iOS? Ask your questions here.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
dburdan
- Posts: 104
- Joined: Fri Jan 28, 2011 5:39 am
Post
by dburdan » Sat Feb 12, 2011 7:03 pm
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.
-
Randy Hengst
- VIP Livecode Opensource Backer

- Posts: 157
- Joined: Thu Jun 29, 2006 4:16 pm
Post
by Randy Hengst » Sat Feb 12, 2011 7:49 pm
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
-
bn
- VIP Livecode Opensource Backer

- Posts: 4166
- Joined: Sun Jan 07, 2007 9:12 pm
Post
by bn » Sat Feb 12, 2011 7:57 pm
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
-
dburdan
- Posts: 104
- Joined: Fri Jan 28, 2011 5:39 am
Post
by dburdan » Sat Feb 12, 2011 8:26 pm
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.
-
neo42
- Posts: 83
- Joined: Tue Mar 01, 2011 10:20 pm
Post
by neo42 » Tue Mar 01, 2011 10:22 pm
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.
-
BvG
- VIP Livecode Opensource Backer

- Posts: 1239
- Joined: Sat Apr 08, 2006 1:10 pm
-
Contact:
Post
by BvG » Tue Mar 01, 2011 11:44 pm
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
...
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
-
jsburnett
- VIP Livecode Opensource Backer

- Posts: 121
- Joined: Fri Mar 09, 2007 9:47 pm
Post
by jsburnett » Wed Mar 02, 2011 11:46 pm
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
-
jsburnett
- VIP Livecode Opensource Backer

- Posts: 121
- Joined: Fri Mar 09, 2007 9:47 pm
Post
by jsburnett » Thu Mar 03, 2011 12:06 am
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