Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

randomSeed: Bad/redundant "if" #3887

Open
igendel opened this issue Sep 28, 2015 · 6 comments
Open

randomSeed: Bad/redundant "if" #3887

igendel opened this issue Sep 28, 2015 · 6 comments
Assignees
Labels
Component: Core Related to the code for the standard Arduino API Type: Bug

Comments

@igendel
Copy link

igendel commented Sep 28, 2015

Hi,

In function randomSeed (WMath.cpp, line 30) there's the code

  if (seed != 0) {
    srandom(seed);
  }

Since "srandom" handles the value 0 itself (see for example its implementation in random.c of AVR-Libc*), it seems there's no need for that "if". Furthermore, it is undocumented and may break functionality - for instance, if a user wants to produce two identical pseudo-random sequences using the seed 0.

If that "if" can be removed, it will save a byte or two.

See discussion about this here (not all of it directly relevant): https://forum.arduino.cc/t/randomseed-why-is-0-ignored/337424

Thanks,

@shiftleftplusone
Copy link

shiftleftplusone commented Sep 29, 2015

For srand(seed) - resp. randomSeed() - , perhaps 0 is handled seperately to avoid division by zero, but often this following is common practice to PRNGs:

seed== 0 // 0: a "real" randomized random seed based on system clock()
seed==-1 // -1: restore last random series
seed== either int value // start PRNG sequence at a pre-defined start value

Note that a PRNG initialized by a certain fixed value will always generate identical pseudo-random series to infinity, which can be used for code testing purposes - but which will not make much sense for common PRNG use!

As Arduino boards don't provide a system clock - and srand(0) or randomSeed(0) are not well-documented by Arduino reference at all - I'm using my own "randomized rand() initialization" by

srand ( (unsigned int)( millis() + AnalogRead(A0) ) );  // as a substitute to srand(0)

_(moreover, the Arduino API function names are not C compliant -
rand(void)
srand(int seed)
had to be used instead, and RAND_MAX should be well-defined!)
_
https://forum.arduino.cc/t/randomseed-why-is-0-ignored/337424/14

@igendel
Copy link
Author

igendel commented Sep 29, 2015

Always nice to read some Vogon poetry ;-)

For the time being, let's separate here the general discussion on PRNGs from the existing Arduino implementation. Given the current state, where randomSeed is essentially a wrapper to AVR-Libc's srandom, the if in the function seems erroneous. Can we do without it?

@shiftleftplusone
Copy link

IMO we actually needed to have srand(0) for "randomized" seeding and srand(-1) to restore the last random series
These C functionalities seems to be missing yet.

@matthijskooijman
Copy link
Collaborator

@igendel, agreed, I cannot see any reason for treating "0" specially. The libc srandom function does not do it, and the randomSeed documentation does not indicate 0 is special either. Since libc says the default seed is '1', the current code means that randomSeed(0), randomSeed(1) and not calling randomSeed() now all result in the same seed, which seems unintended.

@vogonjeltz, you talk about rand() / srand(), which seems to be a different set of functions than the random() / srandom() used by Arduino (though some answers to this post suggest that on Linux, they both access the same random generator, but random() might be better). However, none of these functions actually seem to support 0 and -1 as a special value, AFAICS (Looking at the libc random and rand manpages). Do you have a link to any documentation that describes how these "C functionalities" should work? Though I don't think we would actually need special handling like that - on the Arduino you're in control, so you can pick the random seed (also note that an Arduino has no reliable source of randomness, so implementing automatic randomized seeding seems to be impossible).

@shiftleftplusone
Copy link

shiftleftplusone commented Sep 29, 2015

yes, I only know the common cstdlib C commands rand and srand.
But about the (0) I stand corrected, I muddled it up - it was time(0), not just (0), sorry.
for "randomized" seeds I take

srand ( (unsigned int)( millis() + AnalogRead(A0) ) );

About -1 I'm searching and will report... IIRC I once used it ...

@per1234 per1234 added the Component: Core Related to the code for the standard Arduino API label Jul 4, 2017
@ace-dent
Copy link

ace-dent commented Jun 17, 2021

Proposal from #575:
#define randomSeed(seed) srandom(seed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API Type: Bug
Projects
None yet
Development

No branches or pull requests

6 participants