Random numbers

Hi,
I am somewhat new with C++. I take a semester a year ago and now I am just reviewing my text book. I did this project and looks fine and runs. But the result is not what I am looking for. Every time the program runs, it generates the same random number as if there is a srand() function or a seed value used.
The program must randomly choose 4 numbers from 1 to 25 each time is run. But I always get numbers( 17, 18, 10, 1).

I do not know what I am missing or where it is wrong. Any help will be appreciated. again "this is not a homework project. I am only reviewing what I learned.
Shervin


/*
Date: 1/8/2012
Name: Shervin Ghamghamy
Project: Chapt 3 Project 13
Description: There are four identical prizes to give away
and a pool of 25 finalists. The finalist are assigned numbers from
1 to 25. This program radomly selects the numbers of four finalists to
receive a prize. The program should not pick the same numbers twice.
For example picking finalists 3, 15,22, and 14 would be valid but
picking 3, 3, 3l, and 17 would be invalid.
*/
#include<iostream>
#include<cstdlib>

using namespace std;

int main()

{
int prize = 1;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision (2);

for (int i = 1; i <= 25; i ++)
{

while (prize >=1 && prize <=4)
{
i = 1 + rand() % 25;
cout <<"Contestant number "<<i <<" won prize number " <<prize <<endl;
prize ++;
}

}
return 0;
}
[code] "Please use code tags" [/code]
man wrote:
If no seed value is provided, the rand() function is automatically seeded with a value of 1.
You should put srand( time(NULL) ); at the beggining of main.

Read the manpages and it will state rand() function works hand in hand with srand(...) function. Lesson to learn is need to learn how to read manpages for those functions you want to use.
Topic archived. No new replies allowed.