Hacker News new | past | comments | ask | show | jobs | submit login
Random number generator (included) by language (rosettacode.org)
1 point by pantuza on June 30, 2021 | hide | past | favorite | 2 comments



"State the type of random number generator algorithm used in a language's built-in random number generator. If the language or its immediate libraries don't provide a random number generator, skip this task. If possible, give a link to a wider explanation of the algorithm used."

Fortran has multiple compilers, and the standard does not prescribe the algorithm for RNG to be used. I imagine the situation is similar for C and C++.


I don’t think the C standard says much about its implementation. As far as I can tell

  #define RAND_MAX 32767
  int rand (void){return 0;}
(Edit: changed RAND_MAX from zero. That can’t be zero, but I think rand is allowed to be spectacularly bad)

is a valid implementation.

In real life, implementations are a bit better. The small seed (just a single int) can’t make it much better, though.

The original version also is a LCG, so it has the nasty habit of very short cycle lengths when only looking at least significant bits.

C++ has multiple well-defined engines (https://en.cppreference.com/w/cpp/numeric/random)




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: