CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    [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;}

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: Virtual Slot Machine

    srand() should be called only once - at the beginning of main() - not in the random() function().

  3. #3
    Join Date
    Apr 2009
    Posts
    4

    Re: Virtual Slot Machine

    thank u very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured