coin toss using a class

Jan 17, 2016 at 1:45am
Im having trouble with the random generator. It only displays "heads" first.
I'm also having trouble with the counting how many times each side has been displayed, I know im going to use a for loop but I cant think of how to format it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #include<iostream>
#include<ctime>
#include<time.h>
#include<stdlib.h>
using namespace std;


class coin
{
private:
	int Sideup;
	int heads = 1;
	int tails = 0;

public:
	void flip()
	{
		int srand = (time(NULL)) ;
		int NUM =srand;
		if (NUM = 1, rand()%2)
		{
			
			cout << "heads\n";
		}
		else
		{
			cout << "tails\n";
		}

	}

	int getSideup()
	{
		Sideup++;
		return Sideup;
	}


};

int main()
{
	coin c;
	int n=0;
	//while (n != 20)
	//{
		c.flip();
		c.flip();
		c.flip();
		c.flip();

	//}
	/*cout << "Tails was tossed " << tails << " Sideup" << endl;
	cout << "Heads was tossed " << heads << " Sideup" << endl;*/


	system("pause");
	return 0;
}
Jan 17, 2016 at 2:06am
On a sidenote, not answering your question: just to be cross-platform friendly, use std::cin.get(); instead of system("pause");
Jan 17, 2016 at 2:33am
I just comment that out to do some debugging. I thought "using namespace std" took care of me having to use std
Jan 17, 2016 at 2:36am
Oh, wait I didn't see that statement. It is good to not get into the habit of writing using namespace std;

rather you should do what I do, or write the std:: prefix

1
2
3
4
5
using std::cout;
using std::cin;
using std::endl;
using std::string;
and so on...
Jan 17, 2016 at 2:40am
oh ok, well do you have any insight as to why i keep getting the same output lol I'm a little lost
Jan 17, 2016 at 2:45am
Wait, what I don't get is why you have "rand() % 2" at line 20's if statement. Also, why not just create srand equal to rand()%2 in the first place?
Jan 17, 2016 at 2:50am
yea I changed that when I realized what I did


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<ctime>
using namespace std;


class coin
{
private:
	int Sideup;
	int heads = 0;
	int tails = 0;

public:
	void flip()
	{
		srand(time(NULL));
		int NUM = rand()%2;
		if (NUM = 1)
		{
			
			cout << "heads\n";
		}
		else
		{
			cout << "tails\n";
		}
		

	}

	int getSideup()
	{
		
		Sideup++;
		return Sideup;
	}


};

int main()
{

	coin c;

	c.flip();
	
		
	/*cout << "Tails was tossed " << tails << " Sideup" << endl;
	cout << "Heads was tossed " << heads << " Sideup" << endl;*/


	system("pause");
	return 0;
}  
Jan 17, 2016 at 2:53am
Lol, as long as it worked.
Jan 17, 2016 at 2:54am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <ctime>
#include <cstdlib>

class coin
{
    private:
        int heads = 0;
        int tails = 0;

    public:
        void flip()
        {
            if( std::rand() % 10000 < 5000 ) // we don't want to rely on the last bit being random
            {
                std::cout << "heads\n";
                ++heads ;
            }
            else
            {
                std::cout << "tails\n";
                ++tails ;
            }

        }

        int num_heads() const { return heads ; }
        int num_tails() const { return tails ; }
        int num_flips() const { return num_heads() + num_tails() ; }
};

int main()
{
    std::srand( std::time(nullptr) ) ; // seed once, at the very beginning

    coin c ;

    const int nflips = 20 ;
    for( int n = 0 ; n < nflips ; ++n ) c.flip() ;

    std::cout << "\ntotals - heads: " << c.num_heads() << "  tails: " << c.num_tails() << '\n' ;

    std::cout << "\npress enter to quit" ;
    std::cin.get() ;
}
Jan 17, 2016 at 3:01am
thank you for your help, what was I doing wrong
Jan 17, 2016 at 3:09am
> what was I doing wrong

Re-seeding the rng each time flip is called.
Not keeping track of the number of heads and tails.
Failing to initialise the member variable Sideup.
Relying on the randomness of a particular bit in numbers generated by a poor rng.

Jan 17, 2016 at 3:31am
OK thank you so seeding in done in main to assure its only done once? also what makes a poor rng? I'm asking because the only examples I found were using the modulus 2 form.
Jan 17, 2016 at 3:51am
> seeding in done in main to assure its only done once?

Yes.
Also, components other than main() shouldn't make program-wide policy decisions (like how a shared rng is seeded).


> also what makes a poor rng?

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls).
rand() is not recommended for serious random-number generation needs, like cryptography. It is recommended to use C++11's random number generation facilities to replace rand().
http://en.cppreference.com/w/cpp/numeric/random/rand


A numeric sequence is said to be statistically random when it contains no recognizable patterns or regularities
https://en.wikipedia.org/wiki/Statistical_randomness
Last edited on Jan 17, 2016 at 3:52am
Jan 17, 2016 at 4:14am
ok thanks I guess this is the stuff they don't teach you in school lol I appreciate it
Topic archived. No new replies allowed.