Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Help with true random number generator wanted!

Author
Message
the mook
16
Years of Service
User Offline
Joined: 21st Aug 2008
Location:
Posted: 6th Aug 2009 01:41
Hi!

Just a quick question, hoping people can help me.

I'm trying to seed a truly random number each time my program is run.

I've heard that this can be done off the clock, but when I try it with a 'get time$' command, it only seems to randomise off the hour.

1 - 24 is a little low seed range for me.

Any ideas?

What IS a mook?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 6th Aug 2009 05:55
You won't get a true random number but doing it by the timer is the easiest way to get a better random number.

You do it by TIMER() not by the clock.



You only do it once at the top of your code to make every random number picked after it more random.

ShaunRW
DBPro Developer
17
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 6th Aug 2009 08:14
Whats wrong with using RND( MaxValue )?


Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 6th Aug 2009 09:38
There's nothing wrong with RND() but if you don't use RANDOMIZE TIMER() the program can sometimes pick exactly the same random numbers every time it's ran.

If you use the same seed it produces the same numbers which are perfect for encryption/decryption routines. Not so good for making something you want to be "random" every time.

Run this at least twice:


If you run this program twice (without changing the seed number) in a row, tomorrow, a week from now, or in several years from now it'll still produce the same "random" numbers every time.

And since the TIMER() is a number that started counting up from the minute you turned on your computer... every time it sees RANDOMIZE TIMER() it uses a different seed. The TIMER() is always going up while the computer is on... even if you're not running a Darkbasic program it's still going up.

So that's why we use it... to guarantee that our programs will produce more random numbers every time somebody runs it.

the mook
16
Years of Service
User Offline
Joined: 21st Aug 2008
Location:
Posted: 6th Aug 2009 15:25
Genius! Thank you, folks!

What IS a mook?

Login to post a reply

Server time is: 2025-05-26 06:05:02
Your offset time is: 2025-05-26 06:05:02