You are here:

C++/random number

Advertisement


Question
QUESTION: hi

i have a problem, i have made a dice game witch picks a random number. but the random number only change one's per second, because it's set by the time.



the code:



srand(time(0)); // seeds a random number based on the time



   int randomNumber = rand(); // creates randomized number



   int dice = (randomNumber % 6) + 1;


is there a way to get a random number, there isn't set by the time?

ANSWER: Hi, Mathias.

It is desirable that the random number generator is seeded with the time value.  What I think you are doing wrong is that it sounds like you are seeding the generator every time you use it.  You only want to see the generator once at application launch.  In this way, the starting random number is different every time you run the program.  Every time you call rand, it will generate a different result based on its initial seed and how many times it has been called thus far.

If you have any further questions, please don't hesitate to ask.  I'm here to help. :)

---------- FOLLOW-UP ----------

QUESTION: sorry. but, i need to seed a 1000 numbers in the program, and i want a random number every time i "role".
and i'm looking for a way there isn't using time to seed the random number...

Answer
Hi, Mathias.

I think perhaps you are confused about exactly what seeding the random number generator does.  When you seed the generator, you are simply changing the starting point for the random numbers.  As I am sure you are aware, computers cannot generate actual random numbers -- they can only execute algorithms.  What the random number generator does is output pseudo random numbers based on an algorithm.  The problem is that an algorithm cannot change, so, by default, the random number generator will spit out the same numbers in order every time.

This is where the seed value comes into play.  The seed value is used as a launching point for the random numbers.  Basically, if you provide the same seed every time you launch, you would see the same sequence of numbers come up, just as if you had never seeded the generator at all.  This is why it is useful to use the time function, because the time changes every time you launch the application, meaning you can seed the generator with a different number every time, yielding a different set of "random" numbers.

Remember that you only seed the random number generator once -- at application launch.  Then you call the random number generator function as many times as you like and it will return "random" numbers every time.  See below for reference:

   #include <time.h>
   #include <iostream>
   using namespace::std;

   int rollDice()
   {
       return (rand() % 6) + 1;
   }

   void main()
   {
       srand(time(0));

       for (int i = 0; i < 1000; ++i)
       {
         cout << rollDice() << endl;
       }
   }

Hopefully that clears things up.  Let me know if you have further questions.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.