Thread: Random Number Generator Stuck on 1 Number?

  1. #1
    Registered User
    Join Date
    Nov 2013

    Question Random Number Generator Stuck on 1 Number?

    Hi, I am working with C++ in Visual Studio. It's my first semester doing anything like this ever. Still, I am embarrassed that I am having trouble with this simple "coin flipping" program. The user tells the program how many times to "flip the coin" & then the program tells the user the results of each flip. You'll see I am randomly generating a 1 or a 2 within the function coinFlip to represent heads or tails. However, the problem is that if the user wants more than one coin flip, the "random" number stays the same for all of them, resulting in all heads or all tails. I am thinking this is an issue with the for loop that I have within the function coinFlip, but I cannot figure out what might be going wrong.
    Please help! Thanks! (Code below)

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int coinFlip(int);
    int main()
        int times;
        //ask the user how many times to flip the coin
        cout << "I can flip a coin as many times as you want.\nHow many times should I flip it?\nType the number and press Enter.\n\n";
        cin >> times;
        //explain what just happened
        cout << "\n\nI just flipped a virtual coin " << coinFlip(times) << " times.\n";
        return 0;
    //flip the coin
    int coinFlip(int numTimes)
        //count the number down
        for (int count = numTimes; count > 0; count--)
            unsigned seed = time(0);
            double coin;
            coin = 1 + rand() % 2;
            //display heads if it's 1
            if (coin == 1)
                cout << "\nheads";
            //display tails if it's 2
            else if (coin == 2)
                cout << "\ntails";
            //just in case
                cout << "\nERROR";
        return numTimes;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Call srand exactly once at the start of the main function. As it stands, you're calling it on each iteration with the same seed most of the time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Here is one random number generator with many possibilities, it works great and can be used in your code to get what you want try it :
    using namespace std;
    int main(){
    	srand(time(NULL));						//random numbers that start with current system time
    	rand();									//this line neglects first random number couse its always same
    	float coin;
    	for(int x=1;x<22;x+=1){
    	coin=static_cast<float>(rand())/RAND_MAX;  //converting rand() into float type for decimal numbers
    	coin*=50;                                //range of random numbers (this case 0-50)
    	coin=static_cast<int>(y);				 //converting "coin" into int type of numbers
    Hint: when you set to generate 2 random numbers they will be 0 and 1 not 1 and 2.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    rand();                                 //this line neglects first random number couse its always same
    I ... really? Of course the first random number is not "always the same" (assuming you wait long enough for time() to give a different value).

    And we've (probably) got #include<random> these days which will give you just about anything you want (including 0-50 or, for the OP, a bernoulli trial).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator
    By Unee0x in forum C Programming
    Replies: 3
    Last Post: 03-31-2013, 09:22 AM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. A new random number generator!
    By Sebastiani in forum General Discussions
    Replies: 19
    Last Post: 07-30-2009, 03:27 PM
  4. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM

Tags for this Thread