Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

In which file can I find the calculation that is called with random()? If it is not too much could you also post the content in your answer? I am using an Arduino Uno and its standard IDE.

I found this in the "WMath.cpp" but that is not the final calculation.

void randomSeed(unsigned int seed)
{
  if (seed != 0) {
    srandom(seed);
  }
}

long random(long howbig)
{
  if (howbig == 0) {
    return 0;
  }
  return random() % howbig;
}

long random(long howsmall, long howbig)
{
  if (howsmall >= howbig) {
    return howsmall;
  }
  long diff = howbig - howsmall;
  return random(diff) + howsmall;
}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

The underlying random() function isn't part of the Arduino code. It's actually part of avr-libc. The Arduino IDE only comes with pre-compiled object code for it (as far as I know).

It's open source though. You can get information about it here:

If you look down the page a short way, you'll find the "Source code and documentation" link.

The random() function is under libc/stdlib/random.c. I would paste the source code here, but technically I'd have to include a whole copyright notice with it.

share|improve this answer
    
That are 76 lines with copyright, so why not post it? –  kimliv May 9 '14 at 21:22
    
@kimliv - To be honest, I'd feel discourteous posting a significant piece of someone else's library code without their permission. –  Peter R. Bloomfield May 9 '14 at 22:21
    
But with copyright it is ok an the writer of the library was not the inventor of the algorithm I guess. (What is the main job for me) –  kimliv May 9 '14 at 22:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.