[source code] C Programming Tutorial - 38 - Random Number Generator with rand

+7 Bucky Roberts · August 7, 2014



#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++){
       //returns a random integer from 0 to 32767
       printf("%d \n", rand() );
       //returns a number 1-6
       diceRoll = ( rand()%6 ) + 1;
       printf("You rolled a %d \n", diceRoll );
   }


   return 0;
}



**** CHALLENGE: Create a game where  ****


  1. User rolls three dice

  2. Prints out total

  3. User guess if next roll will be (h)igher, (l)ower, (s)ame

  4. Roll dice again and get total

  5. Guess right? print out “lucky guess”

  6. Guess wrong? print out “you suck!”


Post a Reply

Replies

Oldest  Newest  Rating
0 Maaz Ashraf · November 10, 2015
Hi,

I need help with the Random number generator. I would like to get 0 or 1 and so I am using the code pasted below but I would somehow like to control the number of 1's or 0's I get, eg., I would like to get 30% of the values as 1 but randomly positioned.

Please help. Thanks.

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

int main(){
int i;
int value;

for (i=0; i<20; i++){//print out 20 different random numbers
    value = (rand()%2);//value is either 0 or 1
    printf("%d \n", value);
}
return 0;
}
0 Abishek Dhanpal · November 8, 2015
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int main()
{

    int i=0;
    int dice1,dice2,dice3,dice4,dice5,dice6;
    char player1[20];
    char player2[20];
    int total1=0;
    int total2=0;
    srand(time(NULL));

    printf("welcome to dice_game\n");
    printf("enter player1 name:");
    scanf("%s",player1);
    printf("enter player2 name:");
    scanf("%s",player2);



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

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


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

     total1=dice1+dice2+dice3;
     printf("total1 is: %d\n",total1);

srand(time(NULL));
  for(i=0;i<1;i++){
        dice4=(rand()%6)+1;
        printf("%d\n",dice4);
    }

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


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

     total2=dice4+dice5+dice6;
     printf("total2 is: %d\n",total2);

     if(total1>total2){

        printf("%s has won",player1);

     }

       if(total1<total2){

        printf("%s has won",player2);

     }



       return 0;
}
0 Omar Rios · October 19, 2015
[#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>

/* Player will roll a dice 4 times.
The player has to guess the 4th time if the dice is:
Higher
Lower
Same

Note:
This program was developed because I don't listen to instructions.

*/
int main()
{

int x = 1; //counter variable
int Dice1; //1st roll
int Dice2; //2nd roll
int Dice3; //3rd roll
int Dice4; //4th roll
char NextRoll[1]; //Player's choice
char Roll[1]; //roll dice



// FOR loop to roll the dice the first 3 times
for ( x; x <= 3; x++){

srand(time(NULL));
if (x == 1){
printf(" Type any letter to roll the dice ");
scanf(" %c", Roll);
Dice1 = ( rand()%6 ) + 1;
printf("First roll: %d \n", Dice1);
}else if ( x == 2){
printf(" Type any letter to roll the dice ");
scanf(" %c", Roll);
Dice2 = ( rand()%6 ) + 1;
printf("Second roll: %d \n", Dice2);
}else if ( x == 3){
printf("Type any letter to roll the dice ");
scanf(" %c", Roll);
Dice3 = ( rand()%6 ) + 1;
printf("Third roll: %d \n", Dice3);

}
}
printf(" Is the next roll higher, lower or same? Type 'H', 'L', or 'S' ");
scanf(" %s", NextRoll);
Dice4 = ( rand()%6 ) + 1;
printf("Your pick: %c \n", NextRoll[0]);
printf("Forth roll: %d \n", Dice4);
// test the first 3 rolls against the forth
if ( (( NextRoll[0] == 'H' ) && ( Dice4 > Dice3 )) ||
(( NextRoll[0] == 'L' ) && ( Dice4 < Dice3 )) ||
(( NextRoll[0] == 'S' ) && ( Dice4 == Dice3 ))){
printf("You win!");
}else{
printf("You loose, better luck next time!");

}

return 0;
}
][/code]
  • 1

C

107,156 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator