0

I'm looking for a couple of function that works in a similar fashion of srand(unsigned int) and rand(void) but, with a variable range instead of a fixed one.

I don't like the script

srand(time(NULL));
random_number = rand() % (h_limit - l_limit) + l_limit;

for two reasons. The first one is that a number can be repeated. The second one is that, for large limits, the probability density function is not a costant.

I think that a good solution could be redefine RAND_MAX that is the upper limit of classical functions. Is this a good way to solve my problem or there might be problems? Have you a better way to proceed?

gvgramazio
  • 1,115
  • 3
  • 13
  • 30
  • 1
    If a number can't be repeated, then it is not random. – stark Oct 17 '15 at 16:30
  • @stark: You mean "less random"? – Karoly Horvath Oct 17 '15 at 16:32
  • @KarolyHorvath I guess I mean "not uniformly distributed". It can't be random if it is from a PRNG. – stark Oct 17 '15 at 16:35
  • 4
    If you require that ouputs are never repeated, then you want a pseudo-random permutation function, not a pseudo-random number generator. – user3553031 Oct 17 '15 at 16:35
  • Well, you can build it from a pseudo-random number generator. Or for short lists, just shuffle. – Karoly Horvath Oct 17 '15 at 16:42
  • @stark: Sorry if i caused some misunderstanding. With `rand()`, only the number obtained from the first call is supposed to be "random". The number returned from the second call cannot be the same of the first one. In other words, is the order in which the numbers are given that is random. With the use of `rand()` in the the second line, instead, this property is no more true because there is a modulo operation. – gvgramazio Oct 17 '15 at 16:43
  • @user3553031: you're right, i'll edit the question – gvgramazio Oct 17 '15 at 16:46

0 Answers0