Need help with producing random numbers

Jan 14, 2012 at 12:38am
Ok so i trying to create a random number generator, to generate random lotto numbers, but when i run the program i get the same number for all 5 here is the code i have so far:

//Random number generator for the lottery

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int regNum(int);

int main()
{
char answer;

cout<<" This a Random Lottery Number Generator \n Would you like to continue ? ";
cin>>answer;
if(answer=='Y' || answer=='y')
{
cout<<" Its good that you choose to continue. \n Here are your Regular Numbers";
//displays 5 random numbers for user to apply to lottery
for( int i=0;i<5;i++)
{
cout<<regNum(time(0))<<" ";

}



}else cout<<"Have a good day \n Press Enter to exit.\n \n";
exit(1);
return 0;
}

int regNum(int num )
{
int regNUM;
srand(num);
regNUM=(rand()%56)+1;
return regNUM;
}

can anyone show me the way to 5 different numbers.
Last edited on Jan 14, 2012 at 12:45am
Jan 14, 2012 at 12:41am
Just call srand once at the beginning of main.
Jan 14, 2012 at 12:56am

try this approach and got errors.
Jan 14, 2012 at 1:00am
what kind of errors?
Jan 14, 2012 at 1:08am
it goes to the Header file
Jan 14, 2012 at 1:09am
You are obviously doing something wrong.
Jan 14, 2012 at 1:19am
why yes i am, a guiding light would be helpful
Jan 14, 2012 at 1:40am
Here is your code with the change I was talking about
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
//Random number generator for the lottery

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int regNum();

int main()
{
	srand(time(0));
	char answer;

	cout<<" This a Random Lottery Number Generator \n Would you like to continue ? ";
	cin>>answer;
	if(answer=='Y' || answer=='y')
	{
		cout<<" Its good that you choose to continue. \n Here are your Regular Numbers";
		//displays 5 random numbers for user to apply to lottery
		for( int i=0;i<5;i++)
		{
			cout<<regNum()<<" ";
		}
	}
	else 
	{
		cout<<"Have a good day \n Press Enter to exit.\n \n";
	}
	exit(1);
	return 0;
}

int regNum()
{
	int regNUM;
	regNUM=(rand()%56)+1;
	return regNUM;
}

Jan 14, 2012 at 1:45am
I just learned how to do that a couple of hours ago.
The code is pretty much the same that peter87 wrote.
But if you would like for it to be explained in a video I wil ref you to where i learned it.
There's some very good tuts for beginners like me.

http://thenewboston.org/watch.php?cat=16&number=27
Last edited on Jan 14, 2012 at 1:47am
Jan 14, 2012 at 1:52am
aww man thanks peter that was good stuff i was under the impression you have to pass something to function definition from the function call. now i know better
Topic archived. No new replies allowed.