2

I am new in programming. I need something which can generate random number with C. I found "rand()". But it is not generating random values. Please check the following simple code.

The following code gives

roll the first dice : 6
roll the second dice : 6
roll the third dice : 5

Here is the code:

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


int main()
{

  int dice1,dice2,dice3,total1,total2;
  char prediction[10];

int dice_generator()
{
  dice1= (rand()%6)+1;
  printf("roll the first dice: %d \n", dice1);
  dice2= (rand()%6)+1;
  printf("roll the second dice: %d \n", dice2);
  dice3= (rand()%6)+1;
  printf("roll the third dice: %d \n", dice3);

  return 0;
}

  dice_generator();
  total1 = dice1+dice2+dice3;
  printf("final value is = %d\n",total1);
  return 0;
}
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57
Anshul Vyas
  • 633
  • 9
  • 19

3 Answers3

10

You need to "seed" the random number generator. Try calling

srand(time(NULL));

once at the top of your program.

(There are better ways, but this should get you started.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    Even if using `srand` instead of a serious RNG, I would still seed from `/dev/urandom`. – o11c Jul 10 '15 at 22:03
  • @o11c: Oh, so would I. But the OP said he was "new in programming", so finding a better seed will be a good exercise for another day. – Steve Summit Jul 10 '15 at 22:05
  • Thanks Steve.. I put srand(time(NULL)); and now its generating different numbers. – Anshul Vyas Jul 10 '15 at 22:12
  • 3
    If you find an answer helpful, you should accept the best answer. It's part of the flow of the website and lets future viewers know how to solve the problem :) – Matthew R. Jul 10 '15 at 22:25
3

Firstly, C language does not support nested functions. It is illegal to define dice_generator() inside the definition of main() as in your code. Your compiler might support this, but in any case this is not C.

Secondly, rand() does not generate random numbers. rand() produces a seemingly "erratic" but perfectly deterministic sequence of integers, which begins at some initial number and always follows the same path. All you can do is make rand() start its sequence from a different "seed" number by calling srand with a new seed as an argument.

By default rand() is required to work as if you called srand(1) in order to seed the sequence.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

here is the code, after being corrected so the sub function dice_generator()

is properly separated, rather than buried in main(). (in C, nested functions are not allowed)

the rand() function is properly initialized via the srand() function.

unused variables ( total2 and prediction[] ) are commented out

(another excellent reason to only place one variable declaration per line)

Strongly suggest enabling all the warnings when compiling,

so your compiler can tell you about problems in the code.

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

// prototypes
int dice_generator( void );

 // global data
 int dice1;
 int dice2;
 int dice3;
 int total1;
 //int total2;
 //char prediction[10];

int main( void )
{

  srand(time( NULL ));
  dice_generator();
  total1 = dice1+dice2+dice3;
  printf("final value is = %d\n",total1);
  return 0;
} // end function: main


int dice_generator()
{
  dice1= (rand()%6)+1;
  printf("roll the first dice: %d \n", dice1);
  dice2= (rand()%6)+1;
  printf("roll the second dice: %d \n", dice2);
  dice3= (rand()%6)+1;
  printf("roll the third dice: %d \n", dice3);

  return 0;
} // end function: dice_generator
user3629249
  • 16,402
  • 1
  • 16
  • 17