Jump to content
  • Advertisement
Sign in to follow this  
kev670

slot machine

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a slot machine. at the moment i am trying to have a function for each reel. my broblem is that when i turn them into functions the random numbers are always all the same. For instance 22 or 44. i want my reel to have 10 numbers. here is my code
#include <cstdlib> 
#include <ctime> 
#include <iostream>
using namespace std;
//----------------------------------------------------------------------
void slot1(int random_integer);
void slot2(int random_integer);

//----------------------------------------------------------------------

int main() 
{ 
     
    int random_integer, ri; 
    	

    slot1(random_integer);
    slot2(ri);
    
}
//----------------------------------------------------------------------
void slot1(int random_integer)


    {
	srand(time(NULL));
     	random_integer = (rand()%10)+1; 
     	cout << random_integer;
    } 
//----------------------------------------------------------------------
  void slot2(int ri)


   {
	srand(time(NULL));
     	ri = (rand()%10)+1; 
     	cout << ri;
    }     
       
       
//----------------------------------------------------------------------


i also want to be able to hold a slot and spin the other 2 slots again. how can i do this. thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Call srand() once, at the start of your program.


#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
//----------------------------------------------------------------------
void slot1(int random_integer);
void slot2(int random_integer);

//----------------------------------------------------------------------

int main()
{
srand(time(NULL));
int random_integer, ri;

slot1(random_integer);
slot2(ri);

}
//----------------------------------------------------------------------
void slot1(int random_integer)
{

random_integer = (rand()%10)+1;
cout << random_integer;
}
//----------------------------------------------------------------------
void slot2(int ri)
{

ri = (rand()%10)+1;
cout << ri;
}


//----------------------------------------------------------------------



srand() starts a sequence of random numbers for each unique argument you give it. By calling it with time(NULL) it returns a different sequence every time you run the program. But the chances are that your program will run both functions inside the same second, so time(NULL) will return the same value. so the numbers will be the same.

In general srand() should never be called more than once.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!