die game with random number

This is a discussion on die game with random number within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I studied C/C++ a long time ago, and I want to relearn it...to do so I was trying ...

  1. #1
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    die game with random number

    Hello everyone,

    I studied C/C++ a long time ago, and I want to relearn it...to do so I was trying to do a die game. You enter how many players are playing, and with a loop I generate 2 random numbers for each player...my problem is that the random numbers are the same for all the players... can someone help me on that? here is my code:

    #include<iostream>
    #include<stdlib.h>
    #include<time.h>

    using namespace std;

    int main()
    {
    int player,x,a,b;
    cout<<"number of players: \n";
    cin>>player;

    for (x=1;x<=player; x++)
    {
    cout<<"player"<<x<<"\n";
    srand(time(NULL));
    a=rand() % 6 + 1;
    b=rand() % 6 +1;
    cout<<"your first die is " <<a<< "\n";
    cout<<"your second die is " <<b<< "\n";
    cout<<"your total is "<<a+b<< "\n";
    }

    cin.ignore();
    cin.get();
    return 0;
    }
    thanks for your help

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> srand(time(NULL));

    only do this once (ie: not in the for loop)

  3. #3
    bartinla
    Join Date
    Oct 2004
    Posts
    10

    thanks

    Thanks for your help...it's working great now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 07:04 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21