Jump to content

  • Log In with Google      Sign In   
  • Create Account


Generating Random Number OOP


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 Assassin7257   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 February 2012 - 07:11 PM

#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 ?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9559

Like
1Likes
Like

Posted 18 February 2012 - 07:13 PM

Only call srand() once in your program. Not every time you need a random number.

#3 Assassin7257   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 February 2012 - 08:26 PM

So I need three different member functions for the class then ?

#4 BSt   Members   -  Reputation: 102

Like
1Likes
Like

Posted 18 February 2012 - 08:39 PM

Initialize random number generator

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. ))

#5 Assassin7257   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 February 2012 - 08:52 PM

it crashes when i switch the for statement

#6 Assassin7257   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 February 2012 - 08:53 PM

everytime i run it I get the same number

#7 BSt   Members   -  Reputation: 102

Like
-1Likes
Like

Posted 18 February 2012 - 09:08 PM

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. ))

#8 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 18 February 2012 - 09:09 PM

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]

#9 BSt   Members   -  Reputation: 102

Like
0Likes
Like

Posted 18 February 2012 - 09:12 PM

Removing srand will fix the 'same number every time' problem.

Posted Image .

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

#10 Assassin7257   Members   -  Reputation: 105

Like
0Likes
Like

Posted 18 February 2012 - 09:14 PM

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

#11 BSt   Members   -  Reputation: 102

Like
-2Likes
Like

Posted 18 February 2012 - 09:16 PM

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


( 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


Man, please. Use the minimum of your brain, or don't do things that you don't know.
(( I am learning English. ))

#12 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 18 February 2012 - 09:20 PM

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


Call srand exactly once, at the start of main. When you seed the PRNG, 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.
[TheUnbeliever]

#13 BSt   Members   -  Reputation: 102

Like
-1Likes
Like

Posted 18 February 2012 - 09:30 PM

You are trying to explain more harder things, while he doesn't understand easy things.

He don't know what it does:
int pull = ( (rand() % 3 ) + 1);

the numbers 3 3 2 keep showing everytime


(( I am learning English. ))

#14 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 18 February 2012 - 09:33 PM

Er, I'm explaining his problem.
[TheUnbeliever]

#15 frob   Moderators   -  Reputation: 20250

Like
0Likes
Like

Posted 18 February 2012 - 11:59 PM

Er, I'm explaining his problem.

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:
if(slot1 = slot2 = slot3)


Will not give the expected results.
Check out my personal indie blog at bryanwagstaff.com.

#16 rip-off   Moderators   -  Reputation: 8113

Like
0Likes
Like

Posted 19 February 2012 - 08:10 AM

if (slot1 == slot2 && slot3==slot1 && slot3==slot2) {
// ...
}

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

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS