Random list of numbers

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
   int i;
   int diceRoll;

   for(i=0; i < 20; i++)
   {
       printf("%d \n", rand());
   }

    return 0;
}

This is the code I wrote in c (codeblocks) to get random numbers, the problem is I always get the same sequence: 41,18467,6334,26500 etc...

I'm still learning so please try to explain like you're talking with a 8 year old D:

Upvotes: 4

Views: 5819

Answers (4)

Omid CompSCI
Omid CompSCI

Reputation: 1901

The line srand((unsigned)(time(NULL)) must be outside the loop, must have this line just once in your code.

The modulo rand()%10 means you get any number starting from 0 going up to what you are modulo by -1. So in this case 0-9, if you want 1-10 you do: rand()%10 + 1

int main()
{  
    int i;
    int diceRoll;

    srand((unsigned)(time(NULL));

    for(i=0; i < 20; i++)
    {
        printf("%d \n", rand() % 10); //Gets you numbers 0-9
    }

    return 0;
}

Upvotes: -1

Salah Akbari
Salah Akbari

Reputation: 39946

You get the same sequence each time because the seed for the random number generator isn't set. You need to call srand(time(NULL)) like this:

int main()
{
    srand(time(NULL));
    ....

Upvotes: 5

Shaun Ramsey
Shaun Ramsey

Reputation: 571

Random number generators are pseudorandom. What this means is that they use some "algorithm" to come up with the next "random" number. In other words, if you start with the same seed to this algorithm, you get the same sequence of random numbers each time. To solve this, you have to make sure to seed your random number generator. Sometimes, it is desirable to use the same seed so that you may deduce if the logic of your program is working correct. Either way, one common way that folks seed their programs is through the use of time(NULL). time gives the time elapsed (in seconds) since the epoch time. What this means is that this function changes every second. Thus, if you seed your random number generator with (srand(time(NULL)) at the beginning of the program, you'll get a different random number sequence every different second that you run your program. Be sure not to seed for every random number that you request. Just do this once at the very beginning of your code and then leave it alone.

Your title says C# but I've answered with C++. You'll want to include ctime for this. It may also be beneficial to look at the new style of random number generation as rand() isn't very random these days. Look into #include random and make yourself an engine and distribution to pull random numbers through. Don't forget to seed there as well!

Upvotes: 1

Coffee Maker
Coffee Maker

Reputation: 1573

First of all, seed your random function by including <ctime> and calling srand(time(NULL));.

Secondly, you need a modulo if you're going to call rand(), for example: rand() % x will return a random number from 0 to x-1. Since you're simulating dice rolls, do rand() % 6 + 1.

Upvotes: -1

Related Questions