Sign in to follow this  
Assassin7257

Generating Random Number OOP

Recommended Posts

[code]
#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;
}
[/code]

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 ?

Share this post


Link to post
Share on other sites
[quote]Initialize random number generator[/quote]
[url="http://www.cplusplus.com/reference/clibrary/cstdlib/srand/"]http://www.cplusplus.../cstdlib/srand/[/url]

Also, you should replace:

[CODE]for(int i = 0;i = slotNumber; ++i)[/CODE]
onto
[CODE]for(int i = 0;i == slotNumber; ++i) [/CODE]

[CODE]if(slot1 = slot2 = slot3)[/CODE]
onto
[CODE]if(slot1 == slot2 && slot3==slot1 && slot3==slot2)[/CODE]

Share this post


Link to post
Share on other sites
[quote name='Assassin7257' timestamp='1329619951' post='4914408']
it crashes when i switch the for statement
[/quote]
No, it doesn't.

I have tested out.
[CODE]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;
}
}[/CODE]

Don't "combine/contrive"(?) more, man.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]Removing srand will fix the 'same number every time' problem.[/quote]
[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] .

It can be done using a lot of ways.

Share this post


Link to post
Share on other sites
[code]
int slot::generateSlot()
{
int pull = ( (rand() % 3 ) + 1);
m_slotpicture = pull;
return pull;
}
[/code]

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

Share this post


Link to post
Share on other sites
[quote name='Assassin7257' timestamp='1329621273' post='4914414']
[code]
int slot::generateSlot()
{
int pull = ( (rand() % 3 ) + 1);
m_slotpicture = pull;
return pull;
}
[/code]

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

[quote]( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014[/quote]

Man, please. Use the minimum of your brain, [b]or don't do things that you don't know.[/b]

Share this post


Link to post
Share on other sites
[quote name='Assassin7257' timestamp='1329621273' post='4914414']I changed it to this and now the numbers 3 3 2 keep showing everytime i Run it[/quote]

Call srand exactly once, at the start of main. When you seed the [url="http://en.wikipedia.org/wiki/PRNG"]PRNG[/url], you are saying 'use this as the starting point for the numbers you generate'. So, to get different numbers each time you run the program, you do need to call it. But if you call it every time you generate the number, the problem is you can end up with it being called several times in a row, very quickly (e.g. in a loop, or just because you call generateSlot a few times one after another). This means that the number returned by 'time' is the same each time, which means you say 'start at this number, now give me a pseudo-random number... now start at the same point, and give me a pseudo-random number' - because you started at the same point, you get the same number each time.

So, for the time being - call it once, at the start of your program, then leave it alone. There are circumstances where you might want to reset using a specific seed, but you rarely want to re-seed using the current time.

Share this post


Link to post
Share on other sites
You are trying to explain more harder things, while he doesn't understand easy things.

He don't know what it does:
[CODE]int pull = ( (rand() % 3 ) + 1);[/CODE]
[quote]the numbers 3 3 2 keep showing everytime[/quote]

Share this post


Link to post
Share on other sites
[quote name='TheUnbeliever' timestamp='1329622426' post='4914424']
Er, I'm explaining his problem.
[/quote]
It looks like BSt is suffering from a combination of youth and a really bad translation engine.

Some of his points are still correct, though.

For example, the line:
[color=#000088]if[/color][color=#666600]([/color][color=#000000]slot1 [/color][color=#666600]=[/color][color=#000000] slot2 [/color][color=#666600]=[/color][color=#000000] slot3[/color])


Will not give the expected results.

Share this post


Link to post
Share on other sites
[quote]
if (slot1 == slot2 && slot3==slot1 && slot3==slot2) {
// ...
}
[/quote]
If slot1 equals slot2, and slot3 also equals slot1, then slot3 [b]must[/b] equal slot2. The last clause is unnecessary. You can use:
[code]
if(slot1 == slot2 && slot1 == slot3) {
// ...
}
[/code]
You might prefer to invert the logic:
[code]
if(slot1 != slot2 || slot2 != slot3)
{
// You lose
}
else
{
// You win!
}
[/code]
This may, or may not, make it clearer.

[code]
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;
}
}
[/code]
I know you aren't using this version any more, but I want to make some comments on other aspects of this code that no one has mentioned.

This function returns a reference to a local variable. This is not allowed, it causes undefined behaviour. Also, for certain slot values (maybe ones you didn't plan on using, but you should account for these situations anyway), the function generateSlot() would not return a value. This is also undefined behaviour.

Undefined behaviour is odd in that it can appear to work the way you want it to. But you cannot depend on it "working", and it can and will break as your code evolves.

Both of these situations would have been flagged by the compiler, if you turn your warning levels up high enough (and you should certainly have your warnings on full). Also turn on the option to treat warnings as errors. As a beginner, any warning you ignore is probably a bug in your code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this