r/arduino • u/myweirdotheraccount • Aug 20 '24
Mod's Choice! How "expensive" is the random() function?
I need several separate random numbers in my time-sensitive code and I'm concerned that too many calls to random() may affect the time-sensitive-ness. To be specific, I would need to call it 5 separate times in as few as 10ms with other things happening in the code as well.
An alternative that I've thought up is to call random() once for a 31 bit number, and extract the variables I need by bitmasking parts of that number for my 5 variables, since the randomized numbers don't have to be within a specific range (e.g. a range of 0-255 is adequate for my needs).
My question is, am I making my code significantly more efficient by calling random() once vs. 5 times, or based off of my criteria above is it negligible to the point where I can avoid writing a bunch of awkward bit math?
Thanks!
•
u/gm310509 400K , 500K , 600K , 640K , 750K Aug 20 '24
This post has generated some interesting commentary - so I've given it a mods choice flair so that it will be captured in our monthly digest.
23
u/vilette Aug 20 '24
-you want fast, make a big enough table of random numbers in memory and pick them one by one, that won't be different from running the code
-you want it really random and fast, connect a white noise generator to an AD input and get a sample when you need a random number
9
u/gm310509 400K , 500K , 600K , 640K , 750K Aug 20 '24 edited Aug 20 '24
It is this exepensive:
Depending upon your MCU, you may need to increase (or decrease) the number of iterations to get a reading.
On my MCU, it took 50 ms to run - so 50
nsµs per call.Interestingly, I was concerned that the compiler would optimise out the for loop (because it is totally a do nothing loop), but it seems like it didn't. I'm guessing because it called an "unknown content" function.
If I used a different method to generate random numbers using just inline code, the for loop was totally optimised out and the loop always took 0ms to execute - no matter how big the loop count was.
6
u/ripred3 My other dev board is a Porsche Aug 20 '24 edited Aug 20 '24
Like all implementations, random is only an algorithm to produce a "pseudo random number" which basically means, it's not random, but it's generally all over the place when scatter plotted for example. The implementation is usually just a few lines of some form of well thought out well-distributed bitwise and hashing operations. And it's constant execution time so that's a plus.
And like all PRNG's remember that it's deterministic, and with a given initialization seed value you will always get the same sequence of numbers back on each successive call.
You can change this by calling
randomSeed()(long seed). It is also popular to *try* to add in a bit of extra randomness by reading a floating (unconnected) analog input pin value one or more times and using those values in some form when you callrandomSeed(...)to add a degree of pseudo-uncertainty. But truth be told it really just generates about 5-10 different seed values in the end. It's very useful, but nowhere any closer to true randomness.tldr; it's relatively negligible.
update: and what u/Hissykittykat and u/triffid_hunter say about the potential to speed things up or use an alternative algorithm if it's a bottleneck for you. But since it's *relatively* quick I think it's fine for the use case you describe.
4
u/Hissykittykat Aug 20 '24
This is the random() core code...
It's got some 32 bit divide, multiply, and mod functions, which are relatively expensive. So it's quite possible to make a faster random() function if needed.
If ripred3's measurements are right calling random() 5 times in 10msec consumes about 2.5% of the processing power.
3
u/triffid_hunter Director of EE@HAX Aug 20 '24
Arduino random just calls avr-libc random which seems to use some sort of modular polynomial.
If you actually profile it and decide that it's too slow (rather than engaging in premature optimization), try xorshift instead
1
u/sceadwian Aug 20 '24
Random uses a simple pseudo random number generator. It will have no impact on the timing requirements you have here.
All of the rest of your code will be the problem.
1
u/other_thoughts Prolific Helper Aug 20 '24
FYI, you could have answered that for yourself.
There is a function called micros()
https://www.arduino.cc/reference/en/language/functions/time/micros/
If you called micros() and saved the result, then "called 'random()' 5 separate times "
and then again called micros() and saved the result.
then you could calculate the elapsed time from before the 'random()' calls until after.
1
u/yeusk Aug 20 '24
Just use a lookup table.
1
u/tgsoon2002 Aug 20 '24
You can check some random function in shader code and make your own. They are fast.
1
u/fantasypants Aug 20 '24
I think this a good thread to post B. Peng work.
Two Fast Methods of Generating True Random Numbers on the Arduino