# Random() is much too slow

**URL:** <https://forum.arduino.cc/t/random-is-much-too-slow/1399198>\
**Category:** UNO R4 WiFi\
**Created:** [July 31, 2025, 11:20am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198 "2025-07-31T11:20:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arduinomarc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/arduinomarc/32/1126611_2.png) [@arduinomarc](https://forum.arduino.cc/u/arduinomarc)\
**Post date:** [July 31, 2025, 11:20am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/1 "2025-07-31T11:20:49Z")

</div>

Hi. I make extensive use of the random function, and I noticed that on the Arduino Uno R4 the random() funtion is veeery slooow. Is there a faster replacement for it?

---

<div class="post-metadata">

**Author:** ![Geek\_Emeritus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/geek_emeritus/32/238995_2.png) [@Geek\_Emeritus](https://forum.arduino.cc/u/Geek_Emeritus)\
**Post date:** [July 31, 2025, 11:24am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/2 "2025-07-31T11:24:30Z")

</div>

try getting the current millis(). essentially a random number.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 31, 2025, 11:39am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/3 "2025-07-31T11:39:27Z")

</div>

> [@Geek\_Emeritus](#):
>
> the current millis(). essentially a random number.

You must be kidding …

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 31, 2025, 11:40am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/4 "2025-07-31T11:40:21Z")

</div>

You can have a look at [https://forum.arduino.cc/t/the-reliable-but-not-very-sexy-way-to-seed-random/65872/53](https://forum.arduino.cc/t/the-reliable-but-not-very-sexy-way-to-seed-random/65872/53) from the days of AVR based Arduinos.

I don't have a R4 WiFi so don't know how slow slow is 🙂 So this might be faster and might be better.

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [July 31, 2025, 11:46am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/5 "2025-07-31T11:46:27Z")

</div>

Hi @arduinomarc. There is a discussion on the subject, and explanation, here:

> [@Random function on R4 terribly slow](https://forum.arduino.cc/t/random-function-on-r4-terribly-slow/1342877):
>
> A common use of Arduinos is electronic games. To test such games, it makes sense to replace user inputs by random data using the built-in random function. This works pretty well with Arduino UNO and Arduino MEGA2560. Surprisingly, the latter is 20 percent faster, which I don't fully understand, but it won't give me sleepless nights. When I switched the project to UNO R4 Minima or WiFi, at first it looked like I had bricked the MCU. Only after an endless wait some data was printed on the Serial …

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [August 1, 2025, 12:24am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/6 "2025-08-01T00:24:51Z")

</div>

hardware random number generators typically generate numbers significantly slower than the processor clock rate, although the times mentioned in @ptillisch's link (100ms+) seem quite excessive. ☹

Renesas doesn't say much about their TRNG, nor do they document that library function that the Arduino code uses (which actually fetches 128bits, and throws away the rest) but the TRNG in the SAMD51 says it takes 84 clocks to generate 32bits.  
Renesas considers the TRNG part of their Crypto support, which probably explains the lack of documentation, so it's probably intended for generating one-time keys for use by crypto stuff. ☹  
It doesn't help that the Arduino code "opens" and "closes" the Crypto engine for each random call (and it's not documented how long those take, either!)

> You can disable the use of the TRNG by calling [randomSeed](https://docs.arduino.cc/language-reference/en/functions/random-numbers/randomSeed/)

This is explicit, So you could do something like:

```arduino
   randomSeed(random(MAX_LONG));

```

and subsequent calls to random() would use the (fast) PRNG instead...

---

<div class="post-metadata">

**Author:** ![DVDdoug](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dvddoug/32/671533_2.png) [@DVDdoug](https://forum.arduino.cc/u/DVDdoug)\
**Post date:** [August 1, 2025, 1:04am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/7 "2025-08-01T01:04:28Z")

</div>

If you don't need too many random numbers you can generate them at the top of the program, or in setup(), or a batch of them whenever you have time, and store them in an array.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 1, 2025, 1:55am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/8 "2025-08-01T01:55:52Z")

</div>

```arduino
#include <stdint.h>

uint32_t fast_rand() {
    static uint32_t seed = 2463534242; // you can change this seed
    seed = seed * 1664525 + 1013904223;
    return seed;
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 1, 2025, 6:24am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/9 "2025-08-01T06:24:22Z")

</div>

Your linear congruential generator is extremely fast and requires minimal code, so it is suitable for lightweight applications like basic simulations, simple games, or embedded systems where performance is critical and randomness quality is not.

Studies have shown though that the quality is poor by modern standards. It has noticeable correlations especially in the lower bits.

There are better statistical behavior alternatives ➜ have a look at PCG (Permuted Congruential Generator) and Xoshiro (aka XOR/Shift/Rotate) where Seed initialization must be done externally with good entropy - so you pay the 100ms once or four tines only.

```arduino
uint64_t pcgState = 0x853c49e6748fea9bULL; // your 64-bit seed here

uint32_t pcg32() {
  uint64_t oldState = pcgState;
  pcgState = oldState * 6364136223846793005ULL + 1;
  uint32_t xorshifted = ((oldState >> 18u) ^ oldState) >> 27u;
  uint32_t rot = oldState >> 59u;
  return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}

```

Or

```arduino
uint64_t xoshiroState[4] = {1, 2, 3, 4}; // Replace with four non-zero 64-bit seed values

uint64_t rotl(const uint64_t x, int k) {
  return (x << k) | (x >> (64 - k));
}

uint64_t xoshiro256pp() {
  uint64_t *s = xoshiroState;
  uint64_t result = rotl(s[0] + s[3], 23) + s[0];
  uint64_t t = s[1] << 17;
  s[2] ^= s[0];
  s[3] ^= s[1];
  s[1] ^= s[2];
  s[0] ^= s[3];
  s[2] ^= t;
  s[3] = rotl(s[3], 45);
  return result;
}

```

You must replace the seed values before using these functions.

➜ They can never produce true randomness but can simulate it pretty well for most practical purposes except crypto or fairness-critical tasks as they are designed for speed and good statistical properties but their output is deterministic and can be predicted if the internal state or seed is known.

For cryptography or fairness, you need cryptographically secure random number generators (CSPRNGs) that resist prediction and manipulation and that comes at a cost.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 1, 2025, 6:27am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/10 "2025-08-01T06:27:09Z")

</div>

> [@westfw](#):
>
> although the times mentioned in @ptillisch's link (100ms+) seem quite excessive. ☹

Some true random number generators include built-in delays to enhance security by preventing certain cryptographic attacks. These delays limit how quickly the device produces random values, making it harder for attackers to perform timing analysis or force the generator into predictable states. By slowing down the output rate and controlling access to the entropy source, these delays help protect against side-channel attacks and improve the overall unpredictability and robustness of the random numbers.

➜ this is essential for cryptographic use and I guess that’s why they went for fast random if you build a seed with randomSeed() as most games on Arduino would do so I guess ?

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 1, 2025, 7:03am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/11 "2025-08-01T07:03:22Z")

</div>

Yeah but author didn't say what for he need this generator. May be he is generating nice RGB patterns or something like that, where we don't really need "real randomness".

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 1, 2025, 7:28am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/12 "2025-08-01T07:28:45Z")

</div>

To increase security of a simple linear PRNG, one can use one dedicated low-priority process (on a hrdware which does support multitasking), which maintain a pool of random data, periodically reseeding small, lightweight PRNG. This task can slowly acquire real random data by reading floating analog input, and once read - disturb PRNG sequence flow with this random data. For example by reseeding it or directly XORing PRNG's state structure

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [August 4, 2025, 10:51pm UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/13 "2025-08-04T22:51:04Z")

</div>

> [@J-M-L](#):
>
> Your linear congruential generator is extremely fast and requires minimal code  
> :  
> the quality is poor by modern standards.

It would be nice if assorted Arduinos used identical or at least similar RNGs for the default random() functions.

"Random enough for games and such" is much different than "random enough for cryptography", and there is little point to going to the trouble of a "really good" RNG for most of the things that are likely to be done on Arduino-class boards.

I'll do some experiments with the R4 RNG to see if I can figure out where the bottleneck is...

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [August 5, 2025, 12:41am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/14 "2025-08-05T00:41:38Z")

</div>

Apparently the Arduino trng() function spends most of it's time initializing the R4 SCE, and neither the R4 code nor the Renesas-provided code check whether it has already been initialized ☹

Initializing the SCE drops the random() time to about 20us.

> [@Random function on R4 terribly slow](https://forum.arduino.cc/t/random-function-on-r4-terribly-slow/1342877/12):
>
> I get about 98 ms, pretty consistently. (~97900 us, using micros()) The Arduino code for R4 boards reads: static long trng() { uint32\_t value[4]; if (HW\_SCE\_McuSpecificInit() != FSP\_SUCCESS) return -1; HW\_SCE\_RNG\_Read(value); return (long)value[0] \>= 0 ? value[0] : -value[0]; } Apparently almost ALL of this time is spent in HW\_SCE\_McuSpecificInit(), which doesn't remember that it has already been initialized! (sigh. Cursed vendor libraries!) The time goes down to 20us. That's …

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 5, 2025, 9:11am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/15 "2025-08-05T09:11:40Z")

</div>

Well, as I said - you can combine that simple LC-PRNG with analogRead(). Say, reseed the PRNG, by XORing its state variable (32 bit) with what was read from the floating pin. And if this reseeding would be done in a separate task (on a multitasking MCU) then you'll get zero jitter (time delta between consecutive calls to random())  
Then, you can generate a huge array and check it with online tools for randomness. At least this is what I would do if I need fast & secure PRNG

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [August 5, 2025, 4:36pm UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/16 "2025-08-05T16:36:04Z")

</div>

Ugh. I wish I did not have to write this _yet again_. Using `analogRead` to seed any random number generator is a terrible choice. Please refrain from making that recommendation.

While I have your attention...

> [@vvb333007](#):
>
> To increase security of a simple linear PRNG...

...is simply false. There is no such thing as "increase security" with simple generators. I'm hoping you meant "increase quality".

In addition, neither of those is on-topic for this thread. Please try to stay on-topic.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 6, 2025, 3:26am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/17 "2025-08-06T03:26:15Z")

</div>

> [@Coding\_Badly](#):
>
> any random number generator is a terrible choice.

why? is it not random enough? You can choose floating pin that is far away from pins that are doing something. You'll get less crosstalk on your ADC pin.  
Not arguing, just asking.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [August 6, 2025, 3:44am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/18 "2025-08-06T03:44:59Z")

</div>

> [@Coding\_Badly](#):
>
> In addition, neither of those is on-topic for this thread.

Initial post: "Arduino Uno R4 the random() funtion is veeery slooow. Is there a faster replacement for it?"

So we are on topic. We are trying to find a faster replacement for "random()".

How many clock sources Arduino UNO has? If it is more than one, then one can try to get some entropy from clocks difference. Say, you have 2 independed clock sources: Xtal and RC oscillator. You can compare phase shift between these two. It will give few bits of entropy.

Also, I forgot to mention: if you are reading ADC as entropy source (say, 10 bit) and you have your PRNG state variable of 32 bit, then it would be good idea to duplicate those 10 bits into 30 bits and then XOR with state variable.

It is always a tradeoff between speed and randomness.

If I would need TRNG, then I go with a diode, near it reverse breakdown voltage (may be Zener would work even better). Diode will generate truly random white noise (quantum mechanics agrees with that :)), which is then fed to opamp, or any simple single BJT amplifier and then fed to ADC pin.

This sort of randomness is only needed for secure protocols, like SSL, TLS or IPSec. I don't think TopicStarter implements anything like that.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [August 6, 2025, 4:28am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/19 "2025-08-06T04:28:17Z")

</div>

> [@vvb333007](#):
>
> Initial post: "Arduino Uno R4 the random() funtion is veeery slooow. Is there a faster replacement for it?"
> 
> So we are on topic. We are trying to find a faster replacement for "random()".

Seeding has nothing to do with speed. Secure has nothing to do with speed. Get back on topic. Last warning.

> [@vvb333007](#):
>
> Not arguing, just asking.

In which case, create a new topic and ask.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 6, 2025, 7:50am UTC](https://forum.arduino.cc/t/random-is-much-too-slow/1399198/20 "2025-08-06T07:50:22Z")

</div>

> [@Coding\_Badly](#):
>
> Seeding has nothing to do with speed

Actually it could according to

> [@westfw](#):
>
> > You can disable the use of the TRNG by calling [randomSeed](https://docs.arduino.cc/language-reference/en/functions/random-numbers/randomSeed/)
> 
> This is explicit, So you could do something like:
> 
> ```arduino
> randomSeed(random(MAX_LONG));
> 
> ```
> 
> and subsequent calls to random() would use the (fast) PRNG instead

[Next page](https://forum.arduino.cc/t/random-is-much-too-slow/1399198.md?page=2)
