-
April 17th, 2009, 09:49 AM
#1
[RESOLVED] Virtual Slot Machine
i want to write a virtual slot machine program, in the program i want to write a function which returns a random integer and i should call this function 3 times to get 3 different integers, what happens is that i get the same random integer 3 times, so please i want to know where is the problem in my code. If anyone read c++ for game developing module 1(game institute) he would be familiar with the program its in chapter 3 exercise 3.7.6:
Here is my code:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int random(int low,int high);
int main(){
int a[3];
for(int i=0;i<3;i++){a[i]=random(2,7);}
cout<<a[0]<<a[1]<<a[2];
cin.ignore(cin.rdbuf()->in_avail()+1);
}
int random(int low,int high){
srand(time(0));
int x=low+rand()%((high+1)-low);
return x;}
-
April 17th, 2009, 01:07 PM
#2
Re: Virtual Slot Machine
srand() should be called only once - at the beginning of main() - not in the random() function().
-
April 18th, 2009, 08:43 AM
#3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|