1

I'm trying to get randomly generated numbers by using srand() and rand() but with no avale: every time I run the program it increases by a certain amount of numbers. But if I use a for statement, no pattern can be seen.

For example, if I use the code provided below and run and close the program 10 times, the output will be:

42
52
72
78
85
92
12 (it has reset)
32
48  

Note: one strange thing I noticed about this that when I unfocus or minimize Visual Studio and close the Command Prompt, the next time I run the program the number increases by more than 20, but, if I don't unfocus or minimze Visual Studio the number increases by just little over 1-5. Why's that?

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand (time(NULL));

    int random_number = rand () % 100 + 1;
    cout << "Random number: " << random_number << endl;

    return 0;
}

But, if I use this code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand (time(NULL));

    for (int i = 0; i < 10; i++) {
        int random_number = rand () % 100 + 1;
        cout << "Random number: " << random_number << endl;
    }

    return  0;
}

the numbers don't have a clear pattern and I get the output:

31
10
81
66
74
14
6
97
39
23 

They increase and decrease randomly by random amounts. What seems to be the problem here?

  • 2
    A little unclear. Are you confused because your random numbers are random? – Jason C Sep 16 '16 at 22:20
  • Your second example seems more random because you're re-using the same seed (as you should), rather than generating a new seed based on [almost] the same input in quick succession. – TZHX Sep 16 '16 at 22:22
  • They are random when using the second code snippet, but aren't quite random when using the first code snippet (they increase by a certain amount of numbers and don't decrease; only reset). –  Sep 16 '16 at 22:22
  • But your first set of example numbers doesn't behave as you describe. I see them increasing and decreasing in your list. Your 10-run output list has the same characteristics as your second snippet. It is not increasing by the same amount each time. – Jason C Sep 16 '16 at 22:23
  • @JasonC They reset after hitting 100. –  Sep 16 '16 at 22:24
  • You can use an actual version of random. See this question http://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library. Also, you can have a look at http://en.cppreference.com/w/cpp/numeric/random. – mtb Sep 16 '16 at 22:29
  • 1
    @JasonC Done... –  Sep 16 '16 at 22:35
  • Much better now, although it could benefit from more runs. Another thing you could do is pick a more varying seed, or call rand a few times first to prime it a bit. – Jason C Sep 16 '16 at 22:39
  • @JasonC It means that it increases by a certain amount up until 100ish and then it goes back to the beginning (I can't put it in words). In this case, it has reset back to 12 and will go up to 100 then, again, reset. I don't know how to explain it properly. –  Sep 16 '16 at 22:40
  • Well I hope you're not expecting it to go above 100, you are doing modulo 100. – Jason C Sep 16 '16 at 22:41
  • @JasonC That's the plan. Well, for now, I'll just stick to something more simple and not dive in to difficult functions (as I am a beginner). Thanks for the consideration. –  Sep 16 '16 at 22:44
  • When you ask for *random* numbers, don't be upset when you randomly get a pattern. `0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0` is a perfectly legitimate *random* sequence for any sequence range. – kfsone Sep 16 '16 at 23:10
  • Possible duplicate of [rand() function in C is not random even when seeded](https://stackoverflow.com/q/42380224/608639) and [srand(time(NULL)) generating similar results](https://stackoverflow.com/q/6668282/608639) – jww Oct 17 '18 at 01:13

2 Answers2

4

The first few random numbers returned by some versions of rand (looking at you Microsoft) are highly correlated to the seed they start with, just due to the random number generator formula being used. Since the time isn't changing much between runs, neither do the random numbers. If you throw away the first few random numbers that get returned you can get much better results.

A better solution is to use std::uniform_int_distribution instead of rand.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
-1

Another potential issue is some implementations of rand may interact poorly with certain modulos. For that reason you may want to consider something like this instead:

 int v = ((double)rand() / (double)RAND_MAX) * 100 + 1;

Still not ideal, but easy.

Jason C
  • 38,729
  • 14
  • 126
  • 182