-4

I'm doing an assignment in C++ which has a part in which I need to generate a random number in between 0 and 2. I'm aware that the code to generate this number is

    int randomNumber = rand() % 3;

When I run that in a loop it always generates random numbers. However when I run it independently its ALWAYS 2. I've run a program 30 times with it not in a loop and the randomization is not random. For example

    int randomNumber = rand() % 3;
    int randomNumber1 = rand() % 3;
    int randomNumber2 = rand() % 3;
    int randomNumber3 = rand() % 3;
    int randomNumber4 = rand() % 3;
    int randomNumber5 = rand() % 3;
    cout << randomNumber << endl << randomNumber1 << endl << randomNumber2 << endl << randomNumber3 << endl << randomNumber4 << endl << randomNumber5 << endl;

This will always output 2,2,1,1,2,1

Why isn't this random and how do I make it random?

In case it matters I'm using Visual Studio 2015.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • 2
    1) `rand()` requires seeding with `srand(seed)` 2) `rand()` sucks, you should *never* use it. Use [C++'s RNGs from the `` header](http://en.cppreference.com/w/cpp/numeric/random) instead. – CodesInChaos Oct 14 '15 at 15:42
  • What do you need the random numbers for? – Bathsheba Oct 14 '15 at 15:43
  • 3
    You need to seed using `srand()`, this has been covered numerous times. And now that you know that, forget about `rand()` and start using the types defined in ``; specifically, take a look at `mt19937` and `uniform_int_distribution` – Praetorian Oct 14 '15 at 15:43
  • Duplicate. Has been asked 100s of times. rand() is pseudo rand and needs seeding. Voting to close. – Samidamaru Oct 14 '15 at 15:45
  • `rand()` is quick and easy and perfectly OK for most non-scientific and non-cryptographic purposes. (This is more a "how does `%` work" exercise than a "how to generate good random numbers" exercise.) – molbdnilo Oct 14 '15 at 15:47
  • @molbdnilo It can fail even in very basic scenarios. For example [in some implementations `rand() % 7` is always zero.](http://stackoverflow.com/questions/7866754/why-does-rand-7-always-return-0) – CodesInChaos Oct 14 '15 at 17:06

2 Answers2

1

Pseudo random number generators don't generate real random numbers, but a sequence of numbers which look like they are random. Your implementation of rand() seems to use a pseudo random generator. You'll have to seed it to generate another sequence.

Consider to use std::random_device instead. Most platforms do support non deterministic random sources, so you'll be safe with that most of the time.

cdonat
  • 2,748
  • 16
  • 24
-1

add

#include <ctime>

at the head of your code and call srand((unsigned int)time(NULL)); at the beginning of your code (just after int main(){).

This will have your code generate different sequences unless you execute the code again within 1 second.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Can I create a random number without including another library? – Aaron James Rasmussen Oct 14 '15 at 15:46
  • You *can* create a quick and dirty linear congruential generator. See my answer to http://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-fastest-random-number-generator/31739806#31739806 Note that taking `%` creates a statistical bias, although I doubt %3 is significant cf. problems in the generator itself. – Bathsheba Oct 14 '15 at 15:48