Random woes.

Hey guys! Its my first post (yay!). So I'm trying to create a simple text based game. In this game, you have a squad of policemen and you battle the mafia (meh). I want to generate a random number between a given set of numbers in a for loop, so that each time the loop works, a new number is generated. But I first tried to test out the random number generating function. It doesn't work. Help me with it. Here it is:
P.S. Im a total beginner relying on internet resources to learn C++ and have done everything that lies before arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
//This is what I tried to see if the algorithm worked
//Started right at the for loop

for (int x = 0, int y = y; x == 5; ++x) 
{srand (time (0));

//Random number between 50 and 100

int y = rand() (100 - 50) + 50;
cout << x << endl;
cout << y << endl;
}
return 0;
}
You are restarting the random sequence each time because you are calling srand inside the loop. srand should only be called once, at the start of the program (or at least before you start using rand()).
Also the syntax with your for loop is incorrect. A for loop is used for increments, meaning that you give it a certain starting condition, and it will step through each element of said starting condition until an end condition is met.

What you'd want to do, in this case would be something like
for( x = 0 && y = /*whatever you want*/ ; x < 5 ; ++x )

Also as @Peter87 mentioned, you only need to seed the rand() function once with time(0). Otherwise, you'd be calling the same number pulled from time(0) into your rand() function.
Thanks for all the help guys! So now please tell me that is srand (time (0)) and srand (time (NULL)) the same? And what does this error mean? "rand () cannot be used as a function". Also Peter87 said to use srand () in the start of program. Will it then give unique random numbers between 50 and 100 every time the loop runs or the game starts? Check this code out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{cout << "Welcome to test game!\n";
int inNumber;
int outNumber;
int ex;
srand (time (0));
outNumber = rand () % (100-50)+50;
for (int ex = 0; ex > 5; ++ex) {cout << "ENTER A NUMBER\n";
cin >> inNumber;
cout << outNumber;
cout << ex;
}
return 0;
}

btw Im using codepad.org. Is it a good site? http://codepad.org/ibpW65uZ
the loop doesnt work at all...
Last edited on
So now please tell me that is srand (time (0)) and srand (time (NULL)) the same?

Yes they are the same. You can also write srand(time(nullptr)).

And what does this error mean? "rand () cannot be used as a function"

You could get this error if you declare a local variable named rand. When you later try to call rand it will think you mean the variable and not the function. Calling a variable is of course not possible. To avoid this problem you can name the variable something else and/or write std::rand().

Also Peter87 said to use srand () in the start of program. Will it then give unique random numbers between 50 and 100 every time the loop runs or the game starts?

Yes, but there is no guarantee the values will be unique. It is possible to get the same values again by chance.

srand is used to initialize the starting state of the random number generator. If you pass a fixed value to srand you will always get the same sequence of numbers from rand(). That is why we pass time(0) to srand so that we get a different sequence of numbers from rand each time we run the program.

Note that time(0) usually returns the time in seconds so if you run your program twice within the same second you will get the same sequence of numbers from rand(). This is often not a big deal because we normally don't run programs as often as that.

Check this code out: ...

You never use the ex variable that is defined on line 10.

The loop runs as long as the loop variable ex is greater than 5. This is never the case so the loop never runs.

Im using codepad.org. Is it a good site?

What do you use it for? I strongly recommend you install a C++ compiler on your own computer and use it when writing your programs. codepad.org was a good site for sharing small code snippets with other people but it looks like the C++ compiler is a bit outdated and doesn't support any of the C++11/C++14 features.
Last edited on
Topic archived. No new replies allowed.