randomnumber / randomseed
Dar Scott
dsc at swcp.com
Wed Jul 30 11:44:00 EDT 2003
On Wednesday, July 30, 2003, at 08:31 AM, Björnke von Gierke wrote:
>> I don't know the exact limit for random(), but I do know that 2^31 is
>> a near upper bound. The randomseed limit may be similar. That says
>> nothing on the quality of results for use of random() up to that >
>> limit.
>
> This sees to be true, well actually its (2^31) - 1 probably due to
> starting with zero. how did you come up with that number?
Well, first of all, seeing 32-bit limits all over Revolution and
knowing C programmers often use int when uint is needed and knowing
code is often borrowed from other sources, I suspected 2^32-1 and
2^31-1 as limits from the start.
I then ran this:
on mouseUp
put empty into field "Report"
put 1 into exponent
repeat with i = 1 to 36
put 0 into theMax
put 2^i into range
repeat 100000 times
put random(range) into x
if x > theMax then put x into theMax
end repeat
put i && theMax & LF after field "Report"
end repeat
end mouseUp
I got this on OS X:
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65535
17 131072
18 262144
19 524285
20 1048563
21 2097130
22 4194187
23 8388578
24 16777200
25 33554312
26 67108050
27 134216418
28 268428169
29 536864723
30 1073740741
31 2147446498
32 2147479603
33 2147475667
34 2147465454
35 2147467104
36 2147383474
And noting 2^31 = 2147483648, I saw the discontinuity above. For
larger numbers, you usually won't get the value given to random() but
you still come %-wise close. The exceptions are above 2^31.
I haven't explored random() much more than this. I'm willing to
provide more guesses, if you'd like.
I would use random(99999) in a random sort, such as shuffling cards. I
would use random() in a simple game-type simulation with a small number
of choices. There might be cases where I'd be tempted to use random(x)
where x is up to 10000.
I would think carefully before using random() in fragile probabilistic
simulations.
I would not use random() in cryptography.
I might break my own rules. I've been thinking about a function that
returns a random number [1,2), that is, >=1, <2, and I might use
random() in the first pass. I have an idea for one [0,1) that gets a
little finer as numbers get smaller. I might consider random() as the
second random source for Algorithm M&M which combines random sequences
to make a better one, but if my other source is good, why do I need
random()?
I don't have a good, recent, C-or-unix-oriented source concerning
random numbers that might help you guess the method used in Revolution.
I do have Knuth, which I used for creating random number generators a
quarter of a century ago. I also have some crypto sources, which I
would likely use today. If you are considering rolling your own, I can
point you to sources.
Dar Scott
************************************************************************
****
Dar Scott Consulting http://www.swcp.com/dsc/ Programming
Services
************************************************************************
****
More information about the use-livecode
mailing list