1

I wrote this function to produce random numbers within a certain range. I noticed that it seems to produce negative numbers more when I included a negative range. e.g. -5 to 5

I am assuming this is not random. Is there a problem with this code?

int random_number_generator(int lowest, int highest)
{
    srand((unsigned)time(0));
    int random_integer;
    random_integer = lowest+(rand()%highest);
    cout << random_integer << endl;
}
Marco Lau
  • 579
  • 2
  • 10
  • 25
  • 2
    What arguments are you giving to specify the range? The misleadingly named `highest` is actually the range's size, so you'd need arguments `(-5, 11)` to get the inclusive range `[-5,+5]`. – Mike Seymour Sep 03 '13 at 10:59
  • 2
    Although it doesn't cause this problem, you should only call `srand` *once* at the start of the program. By calling it every time, and only seeding with a new value once a second, you'll keep getting the same "random" numbers. – Mike Seymour Sep 03 '13 at 11:01
  • **Yes**, use this formula: [`(int)((double)rand() / ((double)RAND_MAX + 1) * N)`](http://c-faq.com/lib/randrange.html) – Grijesh Chauhan Sep 03 '13 at 11:01
  • 5
    Note that you must remove your call to srand(). You are supposed to ONLY initialize the seed once in a program. Call srand once somewhere at the start of main(). Your current approach will make rand() give you the same number for every second. Call random_number_generator() 1000 times in a second, it will give you 1000 equal numbers. – nos Sep 03 '13 at 11:01
  • 2
    Since you're using C++ you might want to take a look at the standard library functionality added in the C++11 standard for [pseudo random number generation](http://en.cppreference.com/w/cpp/numeric/random), you might be especially interested in the [`std::uniform_int_distribution`](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) class. – Some programmer dude Sep 03 '13 at 11:03
  • 1
    Also beware of [modulo bias](http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator) (not the problem here, but still an issue) – Hulk Sep 03 '13 at 11:18
  • Even rand returns a signed integer, its value is unsigned. Quote from man 3 rand: "The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX])" –  Sep 03 '13 at 11:46

3 Answers3

5

The desired range would be highest-lowest, so you need to write the expression like this:

random_integer = lowest+(rand()%(highest-lowest));
krisku
  • 3,916
  • 1
  • 18
  • 10
  • 1
    That will give a range that excludes `highest`. The OP seems to be expecting an inclusive range (so that `[-5,+5]` gives as many positive as negative results). – Mike Seymour Sep 03 '13 at 11:05
  • 1
    True. If you want to include the highest value in the potential result, add one to the range, i.e. random_integer = lowest+(rand()%(highest-lowest+1)); – krisku Sep 03 '13 at 11:08
0

You can use this code:

int srandInit = 0;
float Random(float min, float max)
{
    if (srandInit == 0) 
    {
        srand ( (unsigned)time(NULL) );
        srandInit = 1;
    }
    return ((max - min) * ((float)rand() / (float)RAND_MAX)) + min;
}

and just make static_cast from float result to int

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
0

result of next expression is rand number within [ lowest .. highest ] interval

random_integer = lowest + ( rand( ) % (highest-lowest + 1) );
axelBrain
  • 46
  • 2