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.
rand() % nuses only the low bits of the random number whennis significantly smaller thanRAND_MAX. It’s better to use all of the bits e.g. by dividing byRAND_MAXto 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).