Thread: Making A Random Number Each Time?

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Making A Random Number Each Time?

    Ok so I want it so whenever I put in the command roll_dice it will print out 5 random numbers. I figured I was going to run across this problem. But whenever I use the function "roll_dice" it just print's out the same number's each time, like if I print them out twice during the program. Here is the function.

    Code:
    int roll_dice()
    {
    int dice[5];
    srand(time(NULL));
    
    dice[1]=rand()%6+1;
     dice[2]=rand()%6+1;
     dice[3]=rand()%6+1;
     dice[4]=rand()%6+1;
     dice[5]=rand()%6+1;
    
    cout<<dice[1];
     cout<<dice[2];
     cout<<dice[3];
     cout<<dice[4];
     cout<<dice[5];
    }
    It should print out 5 random number's each time I use the function, but it print's out the same ones.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It should print out 5 random number's each time I use the function, but it print's out the same ones.
    That's because you reseed the generator each time you call the function. srand should be called sparingly, usually only once in a short running program.

    >http://members.cox.net/srice1/random/crandom.html
    Hmm, I'm not sure I'd recommend that link...
    My best code is written with the delete key.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I thought it was the link I have at my home computer that teaches better practices for getting more random numbers.... Sorry bout that. Do what Prelude said.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I recommend Prelude's link: http://eternallyconfuzzled.com/articles/rand.html

    Read the whole thing even though your problem is related with the seeding method.

    Also, roll_dice() seems to not return anything. You may want to declare it void, instead of int.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Me
    Join Date
    Jul 2006
    Posts
    71
    Yes, I just figured that out after posting...I hate when that happens, but yes it work's fine now thanks. I'll read the article as well.

  7. #7
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    If you haven't already noticed, you're also trying to access elements 1-5, instead of 0-4.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

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. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM