Thread: Stuck on random generating

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Stuck on random generating

    Hi, im kind of stuck on my coding. The problem im stuck on is that it isn't randomly generating "tails", but all "heads." I'm not sure where my problem so can anybody help or give me tips?

    Code:
    #include <iostream>
    using namespace std;
    int flip (void);
    int main ()
    
    {
    	int t=0, h=0;
    	for (int i=1; i<=50; i++)
    	{
    		{
    		if (flip==0)
    		{
    			cout<<"Tails";
    			t++;
    		}
    		else 
    		{
    			cout<<"Heads";
    			h++;
    		cout<<" ";
    		}
    		}
    		if (i%5==0)
    		cout<<endl;
    	}
    	cout<<endl;
    	cout<<"There are "<<t<<" tails"<<endl;
    	cout<<"There are "<<h<<" heads"<<endl;
    return 0;
    }
    
    int flip (void)
    {
    	if (rand()% 0)
    		return 0;	
    	if (rand()% 1)
    		return 1;	
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You forgot the parentheses after calling Flip:
    Code:
    if (flip()==0)
    Once you do that, you can work on fixing your flip method. You shouldn't ever % 0 (its a divide by zero), and % 1 will always be true, since all integers divide 1 evenly.
    Last edited by jlou; 10-01-2003 at 06:56 PM.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you should seed the random number generator... even though it's only a heads/tails thing... I generally use
    Code:
    srand(clock());
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    thx that helped alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  4. Replies: 7
    Last Post: 09-26-2005, 05:09 PM
  5. Generating a random character/letter, stuck!
    By DanFraser in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2003, 11:30 AM