4
#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:

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    Is this supposed to be C++ code or C#? It appears you're writing C++ code and you have it tagged as C++ but the title and question say C#. – Bennet Leff Jul 25 '16 at 19:01
  • Check this : http://stackoverflow.com/questions/1716308/how-does-a-random-number-generator-work – Рахул Маквана Jul 25 '16 at 19:04
  • [Don't use `rand()`](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – sjrowlinson Jul 25 '16 at 19:08
  • Possible duplicate of [Strange behaviour with rand()](http://stackoverflow.com/questions/4556112/strange-behaviour-with-rand) – J. Chomel Jul 25 '16 at 19:23
  • 1
    I think you should try random in c++11. It's much easier and better. – Yves Jul 25 '16 at 19:43
  • If you were an 8 year old, you should be in bed. ;-) – Rudy Velthuis Jul 25 '16 at 20:02
  • The code is in C I got confused about c# (i just looked on google and C and C# are 2 different things) – Arshdeep Singh Outarel Jul 27 '16 at 13:32
  • Also this is for an exam, so can't use c++ just because it's easier. – Arshdeep Singh Outarel Jul 27 '16 at 13:33
  • @ArshdeepSinghOutarel There is three languages which start with C (C,C++,C#), that's true that *C and C# are 2 different things* but although C and C++ are two different languages but they are very similar in syntax. You can check this link to find [Major differences between C and C++](http://techwelkin.com/difference-between-c-and-c-plus-plus). So I think it will be better if you tag your question with both C and C++ since your problem will be useful for both future C and C++ programmers. – Salah Akbari Jul 27 '16 at 17:03

4 Answers4

5

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));
    ....
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Thanks it works. I'm following a tutorial on youtube, and the guy didn't write that line. – Arshdeep Singh Outarel Jul 25 '16 at 19:13
  • @ArshdeepSinghOutarel that is a good sign you should go looking for a different tutorial. – user4581301 Jul 25 '16 at 20:19
  • My advice is to avoid video tutorials. Instead read an introductory `c++` book from here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – drescherjm Jul 25 '16 at 20:32
  • The guy just made another video for that single line. I think his playlist is messed up the "correct" video was like number 48, and the one i was watching was 11. It was a really old video. – Arshdeep Singh Outarel Jul 27 '16 at 13:31
1

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!

Shaun Ramsey
  • 562
  • 4
  • 14
-1

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.

Coffee Maker
  • 1,543
  • 1
  • 16
  • 29
-1

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;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29