Reputation: 1245
I have looked at how to generate two random ints with rand()%n (where n is the total number of samples) but they seem to usually have a bias.
Is there a better (and preferably simpler) way to generate random numbers between 0 and n (number of samples)?
n is read from a file which contains a list of data points. The goal is to do a line fitting through the data using RANSAC.
Upvotes: 0
Views: 288
Reputation: 42149
rand() % n
uses only the low bits of the random number when n
is significantly smaller than RAND_MAX
. It's better to use all of the bits e.g. by dividing by RAND_MAX
to get a (floating point) number between 0.0 and 1.0 and then multiplying by n and converting back to an integer.
It is also possible that the implementation of rand()
is simply not good enough for some uses. In this case use another random number generator altogether (e.g. Mersenne Twister).
Upvotes: 1