RND or SEEDRND bug?

Previous topic - Next topic

shadok85

Hi all!
I have a problem with the RND function (or SEEDRND, perhaps): whenever I want a random numbers, I pick always the same sequence of digits. Example:

Code (glbasic) Select

LIMITFPS 60
SEEDRND GETTIMERALL()

LOCAL nums%[]
DIM nums%[6]

FOR i=0 TO 5
nums[i] = 1 + RND(90)
NEXT

WHILE TRUE
CLEARSCREEN

IF KEY(57)
FOR i=0 TO 5
nums[i] = 1 + RND(90)
NEXT
ENDIF

FOR i=0 TO 5
PRINT nums[i], 0, i*8
NEXT

SHOWSCREEN
SLEEP 100
WEND


Check by yourself. On the first run, you will got always 39, 9, 15, 20, 71, 14. Why?

Minion

GETTIMERALL() Starts the time from the moment the program started running, so will always (in theory) be the same every time you run. Sadly Im not sure how to get the system time in order to create the random number seed

MrTAToad

I get completely different numbers with each run.  What platform are you running it on ?

Slydog

I think I remember this problem from a few months ago.
On some platforms the result for either SEEDRND() or GETTIMERALL() are 16 bit integers and other platforms are 32 bit integers (or 32 bit and 64 bit).
So on some platforms some bits are shaved off not giving a random seed value.

Try the work around by 'Ocean' in this forum:
http://www.glbasic.com/forum/index.php?topic=4917.msg37548#msg37548
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello


Slydog

#5
Which suggestion on that forum?

Code (glbasic) Select
SEEDRND MID$(PLATFORMINFO$("time"), 17, 2)
This one had a comment: "This works sometimes but there are still often the same results. "

or this one:
Code (glbasic) Select
LOCAL TickCount
INLINE
DECLARE_C_ALIAS(GLBGetTickCount,"kernel32.dll", "GetTickCount",(),int);
TickCount = GLBGetTickCount();
ENDINLINE
SEEDRND TickCount

The second one uses "kernal32.dll" which would only work on a Windows machine.
Is there equivalent INLINE calls for the other platforms?

[Edit]
Here's the English translation if you need it:
http://translate.google.com/translate?js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&sl=de&tl=en&u=http%3A%2F%2Fwww.glbasic.com%2Fforum%2Findex.php%3Ftopic%3D4243.msg31492%23msg31492
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

shadok85

#6
Quote from: MrTAToad on 2010-Sep-23
I get completely different numbers with each run.  What platform are you running it on ?

I'm running it on Vista 32 bit.

Well, I have the free version of GLBasic, so I can't use INLINE. Even if I could use the INLINE command, it's not portable. Instead the snippet made by Ocean works very well. So thank you for your responses!

shadok85

#7
Quote from: Ocean on 2010-Sep-23
For a portable solution you might also want to evaluate this:
http://www.glbasic.com/forum/index.php?topic=4917.msg37547#msg37547

cheers
Ocean

Yeah, now I'm using your snippet, thank you!

MrTAToad

QuoteI'm running it on Vista 32 bit.
Using Windows 7 x64 here...