0

After going through the rabbit hole that is learning about rand() and how it's not very good at generating uniform pseudorandom data based on what I've dug into based on this post: Random float number generation. I am stuck trying to figure out which strategy would yield better balance of performance and accuracy when iterated a significant number of times, 128*10^6 for an example of my use case.

This link is what led me to make this post, otherwise I would have just used rand(): rand() considered harmful

Anyway, my main goal is to understand whether rand() is ever preferable to use over the generator + distribution method. There doesn't seem to be very good info even on cppreference.com or cplusplus.com for performance or time complexity for either of the two strategies.

For example, between the following two random number generation strategies is it always preferable to use the 2nd approach?

  1. rand()
  2. std::mt19937 and uniform_real_distribution

Here is an example of what my code would be doing:

int main(){
      int numIterations = 128E6;
      std::vector<float> randomData;
      randomData.resize(numIterations);
      
      for(int i = 0; i < numIterations; i++){
          randomData[i] = float(rand())/float(RAND_MAX);
      }
}

vs.

#include<random>
int main(){
    std::mt19937 mt(1729);
    std::uniform_real_distribution<float> dist(0.0, 1.0);
    int numIterations = 128E6;
    std::vector<float> randomData;
    randomData.resize(numIterations);
    
    for(int i = 0; i < numIterations; i++){
        randomData[i] = dist(mt);
    }
}
Jinko
  • 311
  • 2
  • 10
  • 2
    as far as I know rand() is faster but less uniform. But easy to measure it. – Botond Horváth Sep 13 '21 at 22:13
  • 2
    Also remember that time complexity depends on where you look at the problem. One could say both examples are O(n) because they both call for a random number once per iteration. – user4581301 Sep 13 '21 at 22:16
  • 2
    Good info on `rand` is scarce because how rand is implemented is almost completely undefined. https://xkcd.com/221/ is a sadly accurate joke. – user4581301 Sep 13 '21 at 22:27
  • 2
    Your question cannot be answered analytically as you haven't provided the source code of the `rand()` and other stuff you're using. So it has to be answered empirically, by measurement. By you. – user207421 Sep 13 '21 at 23:17
  • 2
    If you're looking for ***both*** speed and top-notch performance on statistical tests, consider the [xoshiro/xoroshiro](https://prng.di.unimi.it) class of generators. Using `xoshiro128**` in place of `rand()`, scaled to the range (0,1) and summing the results with all else being equal, ran 3x as fast on my laptop. – pjs Sep 13 '21 at 23:32

0 Answers0