Random algorithm

Jan Schenkel janschenkel at yahoo.com
Thu Nov 13 00:53:59 EST 2008


--- Richard Gaskin <ambassador at fourthworld.com> wrote:
> >>    set the randomSeed to random(4570422)
> > 
> > In theory, that could result in the same series of
> > random numbers multiple times, as the first random
> may
> > start with the same randomseed as another, thus
> > resulting in the same random first number, and
> thus
> > the same second number and so forth.
> 
> I'm pretty sure I'm missing something that will make
> the dim light in my 
> head brighter.
> 
> It would seem that resetting the randomSeed each
> time you use the random 
> function would only have a 1-in-4,570,422 chance of
> getting the same 
> seed as the previous run, no?
> 
> -- 
>   Richard Gaskin
> 

If you set off with the same randomSeed, the random
function will return the same result; ergo you're
resetting the randomSeed with the same number, which
means the next number coming out of the random
function would also be the same.

Try it by making a stack with two buttons.

- The script of button 1
##
on mouseUp
  answer random(50000)
end mouseUp
##

- The script of button 2
##
on mouseUp
  set the randomSeed to 1
  answer random(50000)
end mouseUp
##

Save the stack to disk and open it in a second
Revolution instance. Then click the first button in
both instances, and chances are very slim that you'll
see the same number.

But click on the second button and you'll see two
answer boxes with the number 2082 - at least on my
iMac PPC, you may see a different number but it should
be the same in both instances.
This happens as the same calculation is applied to the
same randomSeed. One implementation that I've seen
involved adding a number and then multiplying with
another number, followed by a modulo operator to make
it fit in the upper limit.

So one shouldn't assume the original random(4570422)
will turn up a different number every time. Had the
randomSeed in the engine been initialized with 1, you
would end up with the same next randomSeed. Lucky for
us, the randomSeed is initialized wioth some magic
formula that makes it fairly unlikely that two
instances churn out the same numbers.

Jan Schenkel.

Quartam Reports & PDF Library for Revolution
<http://www.quartam.com>

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)


      



More information about the use-livecode mailing list