Random generator generates same numbers each time?

The user thinks of a number from 0 to 100 and the program tries to guess it. The problem is, I'm new to random numbers and use a function to generate them that isn't my own. It generates the same numbers each time the program runs, here are screen shots:
Run 1: http://oi60.tinypic.com/35hffnn.jpg

Run 2: http://oi59.tinypic.com/dhekp0.jpg

I read something about seeding or something, if I need it, must don't give me the answer. If you have the liberty of time explain more to me about the random business.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <unistd.h>
#include <string>
#include <ctime>

using namespace std;

int rand_lim(int limit) {
    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);
    return retval;
    }

int main() {
  int count = 0;
  bool repeat = false;
  bool guessedright = false;
  cout << "Please think of a random number between 0 and 100" << endl;
  sleep(1);
  cout << "Did you finish thinking? Press the return key when you are ready";
  cin.get();
  while (!guessedright)
  {
    int random_num = rand_lim(100);
    string current;
    current = random_num;
    string num_try;
    num_try += random_num + " ";
    if (num_try.find(current) != std::string::npos) 
    {
    repeat = true;
    }
    if (repeat)
    {
      continue;
    }
    else
    {
      char a;
      cout << endl << "My guess is " << random_num 
               << ". Is it right, type \'y\' if it is right or \'n\' if it is wrong: " 
               << endl;
      ++count;
      cin >> a;
      if (a=='y' || a=='Y')
      {
	guessedright = true;
      }
      else if (a=='n' || a=='N')
      {
	continue;
      }
      else
      {
	cout << "WHAT was that?? I will take it as wrong anyway!" << endl;
	guessedright = false;
	continue;
      }
  }
    cout << "Yeah! Yeah babe... I WIN!" << endl;
    cout << "But good try man: I had to try " << count << " times." << endl;
    return 0;
}
}


Last edited on
Put this at beginning of your main function. It is from ctime and is necessary to get different random numbers each time.
 
srand (time(NULL));


It generated numbers based off the time from when the program started vs using the same random algorithm each time.
Last edited on
rand() is a PRNG (a "pseudo random number generator")

It basically is a formula which takes an input number 'n', does a series of mathematical computations on the number to "scramble" it, then gives 'n' back as a "random" number, and also uses 'n' for the next iteration of rand().

A simplistic version of rand would be this (note: rand is likely more complicated... and this prng is very crappy. It's just conceptual):

1
2
3
4
5
6
7
int rand_state = 0;

int rand()
{
    rand_state = (rand_state * 15675461) + 18836753;
    return rand_state;
}


Notice that if you call rand(), since the input is 0 the first time... you will get the same output every time since the result of that formula will always be the same.


To solve this problem... you can "seed" the prng, which gives it a new starting point. The function for this is srand(). Conceptually, srand() looks something like this (again, this is oversimplified):

1
2
3
4
void srand(int seed)
{
    rand_state = seed;
}



The usage here is that you would call srand() once at the start of your program with a unique seed. This gives the prng a unique starting point, and will allow rand to generate a different stream of numbers every time.

A typical usage is to use the current time as the seed. Since the system time will be different every time the user runs the program.... it ensures they will have a unique seed.


EDIT: Of course I'm ninja'd by a shorter, more direct answer. But I still feel the above info is useful.
Last edited on
Thanks for posting I actually didn't realize this was the idea behind how it worked. Learned something new.
Last edited on
Thanks everybody, it works!
Topic archived. No new replies allowed.