Page 1 of 1 1
Topic Options
#166125 - 2006-08-20 07:41 PM Problem with SRND and RND
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Using the following code I am not able to retrieve random #'s as I expected. When I call the Function Regenerate() the $Lbl_RNUM[0] works as expected however the $lbl_RNUM[$] always comes back with the same values.

Code:

Function RandomGenerate($howmany,$limit)
Global $output[$howmany]
SRND(@ticks)
$R = Rnd($Limit)

for $ = 0 to $Howmany
While Ascan($output,$r)<0
$output[$] = $R
$R = Rnd($limit)
Loop
Next

EndFunction

Function Regenerate()
RandomGenerate(5,55)
For $ = 0 to 5
$LBL_RNUM[$].Text = $output[$]
Next
RandomGenerate(1,42)
$LBL_RNum[0].Text = $output[0]
EndFunction


_________________________
Today is the tomorrow you worried about yesterday.

Top
#166126 - 2006-08-20 11:49 PM Re: Problem with SRND and RND
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Well I was able to fix it by rewriting the RandomGenerate Function to this...

Code:

Function RandomGenerate($howmany,$limit)
Global $output[$howmany]
SRND(@msecs)
$R = Rnd($Limit)

$= 0
While $ <= $Howmany
If Ascan($Output,$r) = -1
$output[$] = $r
$ = $ + 1
Else
$R = RND($Limit)
Endif
Loop
EndFunction


Top
#166127 - 2006-08-21 10:08 AM Re: Problem with SRND and RND
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Your problem is that you are continuously reseeding the Random Number Generator using the SRND function. This is a very very very bad idea. Don't do it.

You should seed the RNG only once in your script, unless you are deliberately intending to produce repeatable sequences. If you keep reseeding it, you screw up the calculation that makes the RNG appear to return random numbers.

Also, after seeding the RNG you should always discard the first number that RND() returns, as many seeds produce the same first number.

Top
#166128 - 2006-08-21 02:20 PM Re: Problem with SRND and RND
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
That answers my question for today. Thanks
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 699 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.032 seconds in which 0.011 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org