25

In this rather basic C++ code snippet involving random number generation:

include <iostream>
using namespace std;

int main() {
    cout << (rand() % 100);
    return 0;
}

Why am I always getting an output of 41? I'm trying to get it to output some random number between 0 and 100. Maybe I'm not understanding something about how the rand function works?

Deanie
  • 2,316
  • 2
  • 19
  • 35
codedude
  • 6,244
  • 14
  • 63
  • 99
  • 48
    Isn't 41 random enough? I wouldn't have thought of it for sure... – Kerrek SB Dec 15 '12 at 21:39
  • 4
    You forgot to initialize `srand` (http://en.cppreference.com/w/cpp/numeric/random/rand) – Borgleader Dec 15 '12 at 21:39
  • shouldn't it be a different number every time I run it? – codedude Dec 15 '12 at 21:40
  • this is not even compiled! – 0x90 Dec 15 '12 at 21:42
  • 3
    @0x90: Nor is it in fact a "C code snippet"! – Kerrek SB Dec 15 '12 at 21:42
  • @codedude No, it should always return the same value, because if you don't seed the random number generator it get's seeded with a set value. Also, in C++ you should use the `` library instead of `rand()`. – bames53 Dec 15 '12 at 23:45
  • 7
    Maybe your computer is flawed. It thinks it's Deep Thought but keeps getting 41 instead of 42. At least it did it quickly and not in 7.5 million years. –  Jun 07 '13 at 08:42
  • 3
    yet another question for this mysterious language CC++. How come i've only heard of it on SO? – Daboyzuk Jun 07 '13 at 09:11
  • @user2088790: well, we are talking computers, so it is 42 in 0-index :) – Juha Untinen May 11 '16 at 13:17
  • Does this answer your question? [Why does rand() yield the same sequence of numbers on every run?](https://stackoverflow.com/questions/9459035/why-does-rand-yield-the-same-sequence-of-numbers-on-every-run) – Karl Knechtel Jan 22 '23 at 10:50

6 Answers6

36

You need to change the seed.

int main() {

    srand(time(NULL));
    cout << (rand() % 101);
    return 0;
}

This srand thing also works for C.


See also: http://xkcd.com/221/

0x90
  • 39,472
  • 36
  • 165
  • 245
10

For what its worth you are also only generating numbers between 0 and 99 (inclusive). If you wanted to generate values between 0 and 100 you would need.

rand() % 101

in addition to calling srand() as mentioned by others.

DuncanACoulter
  • 2,095
  • 2
  • 25
  • 38
  • Also the numbers won't be uniformly distributed as it's unlikely that rand returns numbers uniformly distributed over an exact multiple of 101. – jcoder Apr 16 '13 at 11:38
7

srand() seeds the random number generator. Without a seed, the generator is unable to generate the numbers you are looking for. As long as one's need for random numbers is not security-critical (e.g. any sort of cryptography), common practice is to use the system time as a seed by using the time() function from the <ctime> library as such: srand(time(0)). This will seed the random number generator with the system time expressed as a Unix timestamp (i.e. the number of seconds since the date 1/1/1970). You can then use rand() to generate a pseudo-random number.

Here is a quote from a duplicate question:

The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

If you want to know more, you can read the following:

http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

Community
  • 1
  • 1
6

You are not seeding the number.

Use This:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    cout << (rand() % 100) << endl;
    return 0;
}

You only need to seed it once though. Basically don't seed it every random number.

4

random functions like borland complier

using namespace std;

int sys_random(int min, int max) {
   return (rand() % (max - min+1) + min);
}

void sys_randomize() {
    srand(time(0));
}
0

"srand(time(NULL));" as 1st line at "main()" won't help you if you're using "rand()" at static init. somewhere. You better create "struct rnd_init { rnd_init() { srand (time (nullptr)); } }" named whatever suits you, as a static var at the scope where "rand()" is being used: at some constructor, or whatever.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33515055) – Fedor Dec 29 '22 at 15:27