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
-
bjb007
- Posts: 313
- Joined: Fri Dec 28, 2007 4:56 am
Post
by bjb007 » Wed Jul 30, 2008 5:59 am
I want to generate 14 random numbers
between 1 and 30 without getting any
repeats.
There's no doubt a simple way but I can't
find it. "Unique" or something similar?
Help appreciated.
Edit: I've put this in the repeat loop so that any
repeating number is discarded and the loop counter
set back by one. Works.
Code: Select all
if tRand is not among the items of field fldRandom then
put tRand & "," after field fldRandom
else
subtract 1 from i
end if
Life is just a bowl of cherries.
-
mokogobo
- Posts: 37
- Joined: Wed Jun 18, 2008 6:27 pm
-
Contact:
Post
by mokogobo » Wed Jul 30, 2008 7:49 am
He's some code that works. Throw it in a button and try it out.
Code: Select all
on mouseUp
-- List of integers to randomize
local integerList
repeat with i = 1 to 30
put i into integerList[i]
end repeat
-- Combine elements as comma separated list of integers.
-- Note that combine doesn't necessarily combine elements "in order"
combine integerList using ","
answer "AFTER combine:" && integerList
-- Sort the elements if you'd like
sort items of integerList ascending numeric
answer "BEFORE:" && integerList
-- Sort items randomly (first pass)
sort items of integerList numeric by random(1000)
answer "AFTER:" && integerList
-- Sort items randomly (second pass)
sort items of integerList numeric by random(1000)
answer "AFTER:" && integerList
-- Sort items randomly (third pass)
sort items of integerList numeric by random(1000)
answer "AFTER:" && integerList
-- Split comma separated list of integers back into an array
split integerList by comma
end mouseUp
The initial sort of the combined array and multiple applications of the random sort are not necessary, but were included to demonstrate some behavior and differences.
I suggest looking up the "combine" and "sort" commands in the Revolution Documentation. There (and below, in short) it describes some high-level operation details.
Note: The order of the elements is not alphabetical or chronological; it is based on the internal hash order of the array. To alphabetize the list, use the sortcommand:
combine monthlyReceivables using return and comma
sort lines of monthlyReceivables by item 2 of each
Good luck.
-
mokogobo
- Posts: 37
- Joined: Wed Jun 18, 2008 6:27 pm
-
Contact:
Post
by mokogobo » Wed Jul 30, 2008 7:57 am
bjb007 wrote:
I've put this in the repeat loop so that any
repeating number is discarded and the loop counter
set back by one. Works.
I'm not certain of the internal workings, but I'd imagine the method you describe (looping until something is found) would be generally less efficient than creating a list of the integers you want (in order), then sorting it randomly, as I did.
They both work, but the trial-and-error approach might not be the wisest of solutions.
-
bjb007
- Posts: 313
- Joined: Fri Dec 28, 2007 4:56 am
Post
by bjb007 » Wed Jul 30, 2008 9:12 am
Thanks for your suggestions mokogobo.
Random numbers are fascinating.
It's said that if a series of numbers are
truly random it can't be proved.
As the Rev docs say - the same seed number
will produce the same series of results
so I added a bit of code to change the
seed before each set of numbers is generated.
Don't know if this makes the numbers
more random though!
Any ideas on how to test for randomness?
Life is just a bowl of cherries.
-
Mark
- Livecode Opensource Backer

- Posts: 5150
- Joined: Thu Feb 23, 2006 9:24 pm
-
Contact:
Post
by Mark » Wed Jul 30, 2008 9:48 am
Hi BJB,
There are statistical tests to test for randomness. Are you sure you really need this?
Getting a unique set of random numbers is simple.
Code: Select all
function uniqueRandom theAmount
if theAmount < 1 or theAmount > 100 then
return empty
else
repeat with x = 1 to 100
put x & cr after myList
end repeat
repeat theAmount
put random(number of lines of myList) into myNumber
put line myNumber of myList & cr after myResult
delete line myNumber of myList
end repeat
return myResult
end if
end uniqueRandom
This script is untested, but even if it contains any errors, I'm sure you get the idea. You could add parameters to this script, to create a different pool from which the random number are drawn, e.g. 1-1000 instead of 1-100.
Note that computers don't generate true random, but it is close enough for many purposes. If you need true random, you can download the rndLib stack from the developers section of the Economy-x-Talk homepage.
Best regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
bjb007
- Posts: 313
- Joined: Fri Dec 28, 2007 4:56 am
Post
by bjb007 » Wed Jul 30, 2008 2:28 pm
Mark
Yes, there is a big difference between
P(seudo)RNGs and the kind used by
online casinos.
Where my RNG prog produces at least
one of every number 0-36 in at most
140 the longest recorded non-appearance
of a number on a real roulette wheel
is 400+.
A big difference.
Life is just a bowl of cherries.
-
Mark
- Livecode Opensource Backer

- Posts: 5150
- Joined: Thu Feb 23, 2006 9:24 pm
-
Contact:
Post
by Mark » Wed Jul 30, 2008 2:39 pm
BJB,
I am not talking of pseudo RNG's and I'm definitely not talking about... I won't even mention it again.
What I am saying is that computer generated random numbers are not random. This is connected to the fact that they need a random seed.
The library on the Economy-x-Talk website, however, does give access to true random numbers.
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
bjb007
- Posts: 313
- Joined: Fri Dec 28, 2007 4:56 am
Post
by bjb007 » Wed Jul 30, 2008 3:33 pm
Mark
Software RNGs are usually referred to
as PRNGs simply because they don't
produce true random numbers.
I'm familiar with random.org which I
believe does produce true random
numbers.
Life is just a bowl of cherries.
-
Mark
- Livecode Opensource Backer

- Posts: 5150
- Joined: Thu Feb 23, 2006 9:24 pm
-
Contact:
Post
by Mark » Wed Jul 30, 2008 3:36 pm
OK, bjb, seems we understand each other, even if using different words :-)
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
Mark Smith
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
-
Contact:
Post
by Mark Smith » Thu Jul 31, 2008 12:26 am
Here's an alternative approach where the randomness is introduced by a 'sort':
Code: Select all
on mouseUp
put randList(1, 30, 14)
end mouseUp
function randList pMin, pMax, pAmount
repeat with n = pMin to pMax
put n & comma after tList
end repeat
delete char -1 of tList
sort items of tList by random(2^31)
return item 1 to pAmount of tList
end randList
Many ways to skin a cat....
Best,
Mark Smith