Random number generator wont work

Ok so i made a random number generator here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

 int random;

 random = srand();

cout << random;

    return 0;
}


the problem is that srand() wont work, rand() works but i cant seem to get srand() to work? what am i missing?
srand does not produce random numbers. It seeds (initializes) the random number generator used by rand.

You do it like this:

1
2
3
4
5
6
7
8
9
10
int main()
{
  // seed the random number generator once and only once
  srand( time(0) );

  // now, when you need random numbers, you can call rand()
  int random = rand();

  cout << random;
}
Cool it worked so how can i make it a little more random?
What do you mean? rand isn't random enough for you, or do you think seeding with the system time is going to make your "random" sequence predictable?
right now its like this 6804, then when i run it again its like 6866 its not random enough, its too preditcible for what i need.
I find it a little implausible that rand is producing predictable results. Just because you ran it twice and both times the numbers were kind of similar doesn't mean they'll always be that way. But whatever.

One thing you could try would be to "scramble" the seed by reseeding with rand output:

1
2
3
4
seed( time(0) ); // initial seed
seed( rand() ); // scramble it

// now try using rand 


Another option might be to throw away the first few outputs of rand:

1
2
3
4
5
6
7
seed( time(0) );

// throw out the first 5 numbers in the sequence
for(int i = 0; i < 5; ++i)
  rand();

// now try using rand 



Of course, these "fixes" are completely superfluous and will do very little (read: nothing) to make the rand sequence more random. All they do is change where the sequence starts.
Last edited on
Ok now when i try it in this 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
//Random Number Generator
    srand(time(0) );
    int random = rand();
    //End of random number generator


    if(JobChoice == "A" || JobChoice == "a"){

        cout << "Our printers broke so we need you to " << endl;
        cout << "type out these codes for us as many times as you can." << endl;
        cout << random;
        cout << "\n";
        getline(cin, Typing);
    }
            while(JobChoice != "Back" || JobChoice != "back"){
            getline(cin, Typing);

                if(Typing == random){
                money +=7;
                cout << "Money +7 " << money << endl;
                cout << "\n";
                }

                    if(Typing == "Back" || Typing == "back"){
                    MainMenu();
                    }


what i want to do is have the number generator generate a random number which it does, then i want the user to have to type in what was generated to earn money, but it wont work and i dont get why? it the line: if(Typing == random) i thought its basically saying if what you type in (Typing) equals what was generated give $7. Why wont it work?
Typing is a string and random is an int. They're two different types, so you can't compare them like that.

If you're inputting a number, one option is to take the input as an integer instead of using getline.

1
2
3
4
5
6
int userguess;

//...

cin >> userguess;
if(userguess == random)  // this will work, because both random and userguess are ints 
Topic archived. No new replies allowed.