Random algorithm
Dave Cragg
dave.cragg at lacscentre.co.uk
Sat Nov 15 03:55:36 EST 2008
On 13 Nov 2008, at 19:38, Richard Gaskin wrote:
> So unless I'm missing something obvious (and it certainly wouldn't
> be the first time), beginning with a fresh seed as Rev does and then
> resetting it each time during the session seems a fair way to avoid
> discernible reproducible patterns for most applications.
Sorry for going back to this, but I'd just like to repeat my concern
for resetting the seed using a "random" value. I may have
misunderstood what "resetting it each time" means. In some posts
people have talked about resetting it before "each run" and others
have talked about resetting before each call to the random function.
My concern was with a particular "each run" situation.
Say the task is to produce 5000 sets of 5 random numbers from 1 to
1000. (perhaps for a gaming task) The straightforward way to do this
is to use the default Rev seed and call random(1000) 25000 times,
dividing the results into 5000 sets of 5.
An alternative would be to reset the randomSeed before generating each
set of five numbers. At first glance, this might seem like a
reasonable thing to do. But depending on how the seed is set, it could
produce unwanted results. In the second script below, the seed is
reset using the random of an incremented number. (4570422 incremented
by 1 to avoid getting stuck with the same seed)
The two scripts measure the number of repeated sequences that are
generated by the two methods. The first method has yet to produce a
repeated sequence here. But the second produces repeated sequences on
almost every run. So by resetting the seed for "each run", the
occurrence of a repeated sequence changes from a highly improbable
event to a fairly safe bet.
Cheers
Dave
METHOD 1 (no resetting)
on mouseUp
repeat 5000
put empty into tVal
repeat 5
put random(1000) & "," after tVal
end repeat
add 1 to tValArray[tVal]
end repeat
put 0 into tCount
repeat for each element tEl in tValArray
if tEl > 1 then
add 1 to tCount
end if
end repeat
put tCount
end mouseUp
METHOD 2 (reset on every "set")
on mouseUp
-----------------------
put 4570422 into tSeedBase
repeat 5000
put empty into tVal2
add 1 to tSeedBase
set the randomSeed to random(tSeedBase)
repeat 5
put random(1000) & "," after tVal2
end repeat
add 1 to tValArray2[tVal2]
end repeat
---------------------------
put 0 into tCount2
repeat for each element tEl in tValArray2
if tEl > 1 then
add 1 to tCount2
end if
end repeat
put tCount2
end mouseUp
More information about the use-livecode
mailing list