Function Rand() displays same numbers every time.

0 Sha Kal · August 3, 2015
Its self explainatory, every time i run rand() it shows me the same numbers, as if computer allredy decided which numbers is it going to show if i run rand, every time...

I just want to run simple piece of code like: 

printf (" %d\n",rand()); 

It always prints out 41. 

Anything i do not know here ^.- ?

Post a Reply


Oldest  Newest  Rating
0 Jon Z. · August 4, 2015
0 Sha Kal · August 3, 2015
I searched a bit and i find out and implemented this "  srand ( (unsigned)time(NULL)); "  into my loop. I think it sets the new value to my variable based on time being. Not completely sure but it works!
0 Franz Schmidt · August 3, 2015
Yeah you forgot something. The random function is not that easy to handle:

First of all you need the srand() function. This will generate a so called seed for your rand generator. (This is necessary to generated really random numbers. If you don't do this and you compile your code and run it you will always get the same row of numbers (not the same like you did))

And the reason why you always get the same number is that you didn't established the borders or basically the pool of numbers where the randomizer pics his numbers out. Don't know why its 41 but will depent on your computer and the compiler or the buffer where he gets the values from and probably isn't static at all.

Hope it helps and keep on working :D
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator