retrieving the random seed

General FreeBASIC programming questions.
Post Reply
KlausPeterVDL
Posts: 45
Joined: May 04, 2014 21:02

retrieving the random seed

Post by KlausPeterVDL »

I searched here and on the wiki and the answer seems to be that there's no way to retrieve/save/load the seed used by the built-in random generator. I have no problem cooking up something else, just wondering if i'm overlooking something though. thanks for any pointers!

edit p.s. also, is it true that #if/#ifdef's can't tell if a program has been compiled as -s gui or -s console with or without tricks that may be different for linux and windows?
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: retrieving the random seed

Post by dkl »

The seed for the Rnd() function can be set by using Randomize, for example:

Code: Select all

'' At start of program, seed the random number generator based on the current timer():
randomize( timer( ) )
Initializing it with Timer() is probably the most common way to do it in FB programs.

There is no way to retrieve the seed from the FB runtime. FB supports various random number generator algorithm, the seed given via Randomize() is used differently for each one, including being passed on to operating system functions...

---

There is indeed no built-in symbol to check with #if or #ifdef that tells whether -s console or -s gui was used. However you could make one manually by using the -d foo fbc command line option:
fbc foo.bas -d something

Code: Select all

'' foo.bas
#ifdef something
'' do something
#endif
-s gui/console only has an effect on Windows anyways: If I remember correctly, -s console sets a flag in the generated .exe file that tells the operating system to create a console window, -s gui prevents that flag from being set so there won't be a console window opened. In the ELF executable format used on Linux or BSD systems there is no such thing, and fbc ignores the -s options for those targets.
KlausPeterVDL
Posts: 45
Joined: May 04, 2014 21:02

Re: retrieving the random seed

Post by KlausPeterVDL »

dkl wrote:..There is no way to retrieve the seed from the FB runtime. FB supports various random number generator algorithm, the seed given via Randomize() is used differently for each one, including being passed on to operating system functions...
cool, thanks for the confirmation!
i'll hook up something of my own then so i can keep the sequence btwn save/load.

---
dkl wrote:There is indeed no built-in symbol to check with #if or #ifdef that tells whether -s console or -s gui was used. However you could make one manually by using the -d foo fbc command line option:
fbc foo.bas -d something
apologies, i did not see that, " -d isperfect! "

dkl wrote:In the ELF executable format used on Linux or BSD systems there is no such thing, and fbc ignores the -s options for those targets.
that's good to know, saves me 1/4 effort in a way ;) .. i take it i'll run " $ program& " to make program act like it would on windows? wait, let me say that another way, if i want to have a gui that logs to the console if there is one, can i somehow detect that i started it in " $ program& " mode (or from an icon), effectively telling it (the debug build) to be silent then?

command-line options are fine of course, just wondering about the metal

-d
/does a happy dance.

thanks again!
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: retrieving the random seed

Post by dkl »

Well, if you're starting a program from a terminal but don't want to see its output, you could probably do something like this:
$ ./myprogram > /dev/null
When starting from the desktop GUI or file manager, the output is typically lost because the desktop/file manager don't preserve it anywhere (I think only stderr output is typically written into the ~/.xsession-errors file to allow at least retrieving the error output of the installed applications even if not started from a terminal).

Personally I'd prefer if programs have a -v or -debug command line option, allowing the user to enable the debug output if wanted. In FB command line options can be implemented by checking the Command() function, for example.

If needed, it is possible for a program to check whether it's running in a terminal or not - but currently there is no built-in function available in FB to do that. There's only an undocumented internal FB function, and the possibility to call the appropriate system functions (isatty() on Linux/etc., GetConsoleMode() on Win32) manually.
KlausPeterVDL
Posts: 45
Joined: May 04, 2014 21:02

Re: retrieving the random seed

Post by KlausPeterVDL »

dkl wrote: the possibility to call the appropriate system functions (isatty() on Linux/etc., GetConsoleMode() on Win32) manually.
cool. i can wrap these and be on my way. tyvm!
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Re: retrieving the random seed

Post by rolliebollocks »

From what I understand, calling the timer is redundant.

RANDOMIZE itself generates unique outputs each run, right?
gothon
Posts: 225
Joined: Apr 11, 2011 22:22

Re: retrieving the random seed

Post by gothon »

rolliebollocks wrote:From what I understand, calling the timer is redundant.

RANDOMIZE itself generates unique outputs each run, right?
Yes, that is correct. Although the redundancy is only in the 6 characters of extra code it takes to write it out, since according to the documentation Randomize uses Timer to produce the seed value in that case as-well.

http://www.freebasic.net/wiki/wikka.php ... gRandomize
Randomize [ seed ][, algorithm ]

Parameters:
seed
A double seed value for the random number generator. If omitted, a value based on Timer will be used instead.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: retrieving the random seed

Post by TESLACOIL »

I have a long lists of random numbers stored in *.txt files

There are also mathematical code methods for creating pseudo random numbers if you do not want to load and save external files.

http://en.wikipedia.org/wiki/Pseudorand ... _generator

http://www.random.org/randomness/
Post Reply