Generating uniformly distributed random numbers within a range in C

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I know there are similar questions.
But mine has different settings.

    • Have to use rand()
    • Have to use srand() to take a seed from user input
    • Only using standard library

I don t know if it s possible to get uniformly distributed random numbers, do I have to use the random library?

Answers

The C++ standard mostly defers to the C standard when it comes to the heritage functions like rand() and srand().

Unfortunately, there is nothing in the C standard about rand() delivering uniform distribution. In fact, it states in a footnote (my emphasis):

There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs.

If you want specific properties for your random numbers, you really should be using the C++ stuff in <random> (yes, I know you appear to have discounted that but you may have to make a trade-off: either use them or have no guarantee of a specific distribution).

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/33005916/generating-uniformly-distributed-random-numbers-within-a-range-in-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils