Generating Random Number OOP

Started by
14 comments, last by rip-off 12 years, 1 month ago

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

class slot
{
public:
int m_slotpicture; // determines if slot winner

int& generateSlot(int slotNumber); //generate what slot picture is shown

};


int& slot::generateSlot(int slotNumber)
{
for(int i = 0;i = slotNumber; ++i)
{
srand(time(0));
int slot = rand();
int pull = ( (slot % 3 ) + 1);
m_slotpicture = pull;
return pull;
}
}

void checkWinner(int& slot1, int& slot2, int& slot3)
{
if(slot1 = slot2 = slot3)
{
cout << "Congradulations ! You've WON !" << endl;
}

else
{
cout << "You didn't win..." << endl;
}
}

int main()
{
slot slot1;
slot slot2;
slot slot3;

int firstSlot = slot1.generateSlot(1);
int secondSlot = slot2.generateSlot(2);
int thirdSlot = slot3.generateSlot(3);

cout << "The first slot was " << slot1.m_slotpicture << endl;
cout << "The second slot was " << slot2.m_slotpicture << endl;
cout << "The third slot waas " << slot3.m_slotpicture << endl;

checkWinner(firstSlot, secondSlot, thirdSlot);

return 0;
}


I want to try making a slot machine game this time using objects but everytime I render I get the same number how would I do it that I get a different number everytime ?
Advertisement
Only call srand() once in your program. Not every time you need a random number.
So I need three different member functions for the class then ?
Initialize random number generator[/quote]
http://www.cplusplus.../cstdlib/srand/

Also, you should replace:

for(int i = 0;i = slotNumber; ++i)
onto
for(int i = 0;i == slotNumber; ++i)

if(slot1 = slot2 = slot3)
onto
if(slot1 == slot2 && slot3==slot1 && slot3==slot2)
(( I am learning English. ))
it crashes when i switch the for statement
everytime i run it I get the same number

it crashes when i switch the for statement

No, it doesn't.

I have tested out.
int& slot::generateSlot(int slotNumber)
{
for(int i = 0;i = slotNumber; ++i)
{
int slot = rand();
int pull = ( (slot % 3 ) + 6);
m_slotpicture = pull;
return pull;
}
}


Don't "combine/contrive"(?) more, man.
(( I am learning English. ))
Just to explain the previous post: '=' is assignment but, (in)conveniently also returns the assigned value which can then be converted to bool. '==' is comparison. You also don't want to return a reference in generateSlot - that variable is local to the loop, and goes out of scope at the end of the curly bracket. Its value needn't persist between iterations of the loop, nor after the function returns. In practice, it might seem to work for now, but it can, does, and will break.

You seem to use references in a lot of places you don't need to, actually. You might be trying to optimize, but for built-in types the difference is likely to be either none at all or negligible. You want to use const reference for that, anyway - non-const reference lets you directly modify the argument, which is poor documentation for anyone using or reading your code, apart from anything else. If you need a copy, just pass by value.

That for loop, though, is the real problem. Now that you've 'fixed' the condition to be '==', it will first set i to 0, then check if i and slotNumber are equal. If slotNumber is 0, it'll execute the first iteration of the loop and return immediately (i.e. without actually looping). If slotNumber isn't 0, the function will return without actually returning a value. Except that the calling code doesn't know this, so it'll look for the return value and take whatever garbage value it finds there. Since generateNumber returns a reference, this probably means it later tries to read from an address that it doesn't have access to, and crashes with an access violation or segmentation fault. (When you say 'it crashes', you need to give us more detail about how, and what information you get when it does so - but I'm assuming it's one of these two.)

I think you want to delete the for loop entirely, and remove the slotNumber parameter which seems completely superfluous. Removing srand will fix the 'same number every time' problem.
[TheUnbeliever]
Removing srand will fix the 'same number every time' problem.[/quote]
smile.png .

It can be done using a lot of ways.
(( I am learning English. ))

int slot::generateSlot()
{
int pull = ( (rand() % 3 ) + 1);
m_slotpicture = pull;
return pull;
}


I changed it to this and now the numbers 3 3 2 keep showing everytime i Run it

This topic is closed to new replies.

Advertisement