Jump to content

Generating random numbers!!

- - - - -

  • Please log in to reply
15 replies to this topic

#1
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts
The computer will generate six numbers between 1-40 inclusive.
Then i will have to bet six numbers between 1-40 inclusive

If I get all the six numbers correct, I win the jackpot
If I get five correct numbers, I win RS 10000
If I get four correct numbers, I win RS 4500
and finally if I get three correct numbers, I win RS 100

I want to know what are the codes should I need and how to generate the numbers.
Can someone help me to solve this?? (The code will be written in C program)

Thnks

#2
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
rand()%41 - this command will generate random numbers(from 1 to 40) - you have to save them into an array.
scanf(); - use this to read the numbers,then just check if the pressed number is into the array.

#3
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts

alex1 said:

rand()%41 - this command will generate random numbers(from 1 to 40) - you have to save them into an array.

I think you forgot about '+'. In my opinion rand() % 40 +1 will generate random numbers from 1 to 40.

As for the main toppic. It would be nice to see what have you done so far.
Basicly you need 2 arrays one for a user to fill and one for program to fill with random numbers. Then compare arrays and finally check how many numbers in first array matches second array. Also You might want to check If User or Program didn't put two the same numbers.
Remebre about KISS & DRY

#4
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts
Ya, i have understand the concept but I don't know
how to write the codes that why I want someone to help me
in writing the codes??

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Someone else posted the exact same problem in this thread. You may want to look there, as well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts

kirikou2 said:

The computer will generate six numbers between 1-40 inclusive.
Then a player will have to bet six numbers between 1-40 inclusive

If he gets all the six numbers correct, he wins the jackpot
If he gets five correct numbers, he wins RS 10000
If he gets four correct numbers, he wins RS 4500
and finally if he gets three correct numbers, he wins RS 100

The program is below, the fact is when the six numbers which I entered sometime matches
completely to the generated number by the computer but it does not display any prices even
for five,four or three correct numbers

Can someone help me to solve this??

Thnks



#include <stdlib.h>

#include <stdio.h>


int main()

{

int numContainer[6];

int theBet[6];

int count = 0; //for counting the same number


//this is for the player to bet six number

int i;

printf("Please bet 6 numbers in the range 1-40: ");

scanf("%d",&numContainer[6]);

printf("\n");


//this is for the number generator

for(i=0;i<6;i++)

{

theBet[i] = (rand()%40)+1;

printf("%d: ",theBet[i]);

}


int j;

//checking numbers, if there is a same number then add to count

for(i=0;i<6;i++)

{

for(j=0;j<6;j++)

{

if(numContainer[i] == theBet[j])

{

count++;

}

}

}


//the prize for how many same numbers

if(count == 6)

{

printf("Win Jackpot!!");

}

else if(count == 5)

{

printf("Win RS 10000");

}

else if(count == 4)

{

printf("Wind RS 4500");

}

else if(count == 3)

{

printf("Win RS 100");

}



system("PAUSE");

return(0);

}


#7
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
printf("Please bet 6 numbers in the range 1-40: ");

scanf("%d",&numContainer[6]);

printf("\n");

This should be inside a loop,because now the numcontainer have just 1 proper value inside.

somthing like:
for(i=0;i<6;i++){

printf("Please bet the %d number between 1-40: ",i+1);

scanf("%d",&numContainer[i]);

printf("\n");

}

You're on a good way!

#8
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts

Quote

The code do work. Now I have created additional code such that a bet costs Rs 20
and a player can bet as many time as he wishes. Then the total cost of the bet is displayed.
The problem which am encountered here is that, suppose I bet 3 time, then the program
should allow me to bet six numbers three time, but in this case the program allows me to bet only one time!!

How to solve this???

#include <stdio.h>

#include <stdlib.h>


int main()

{


   int numContainer[6];

int theBet[6];

int count = 0; //for counting the same number

int i,e,f;


printf("How many time you want to bet:");

scanf("%d",&e);

f=e*20;

printf("Your total cost is: %d\n",f);

printf("\n");

//this is for the player to bet six number

for(i=0;i<6;i++){

printf("Bet six numbers between 1-40: ",i+1);

scanf("%d",&numContainer[i]);

printf("\n");   

}

for(i=0;i<6;i++){ 

                              

theBet[i] = (rand()%40)+1;

printf("%d: ",theBet[i]);

}

//checking numbers, if there is a same number then add to count


int j;

for(i=0;i<6;i++){

                 

for(j=0;j<6;j++){

if(numContainer[i] == theBet[j])

{

count++;

}

}

}


//the prize for how many same numbers

if(count == 6)

{

printf("You Win the JACKPOT!!\n");

printf("\n"); 

}

else if(count == 5)

{

printf("You Win RS 10000\n");

printf("\n"); 

}

else if(count == 4)

{

printf(" You win RS 4500\n");

printf("\n"); 

}

else if(count == 3)

{

printf("You win RS 100\n");

printf("\n"); 

}

else if(count<3)

{

printf("Sorry,You Lose\n");

printf("\n"); 

}

system("PAUSE");

return 0;

}


#9
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
for(i=0;i<6;i++){

printf("Bet six numbers between 1-40: ",i+1);

scanf("%d",&numContainer[i]);

printf("\n");   

}
now the i+1 into the printf is useless :cool:

#10
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts
Even though I have removed the i+1 in the printf
it does not work
for example if i want to bet 3 time, the program should allow me to enter six numbers
3 times but it doesn't despite i have removed the i+1

How to solve this now???

#11
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
for(i=0;i<e;i++){

printf("Bet numbers between 1-40: ");

scanf("%d",&numContainer[i]);

printf("\n");   

}

it was:
for(i=0;i<6;i++)
since you say i<6 - isn't it logic to make 6 loops?You need to replace the 6 with the number entered by the user - at your case the loop should be done e times.

Try to read the code line by line and you will find the solutions.

#12
kirikou2

kirikou2

    Newbie

  • Members
  • Pip
  • 7 posts
Now, Every time when am compiling the program below it always generate the same
random numbers that is 2 28 15 21 10 5
How to make the program generate other numbers??

#include <stdlib.h>

#include <stdio.h>


int main()

{

int numContainer[6];

int theBet[6];

int count = 0; //for counting the same number

int i,e,f;


printf("How many time you want to bet:");

scanf("%d",&e);

f=e*20;

printf("Your total cost is: Rs %d\n",f);

printf("\n");


//this is for the player to bet six number

for(i=0;i<6;i++){

                 

printf("Bet six numbers between 1-40: ");

scanf("%d",&numContainer[i]);

printf("\n");   

}

for(i=0;i<6;i++){ 

                                               

theBet[i] = rand()%40+1;

printf("%d:",theBet[i]);

}


//checking numbers, if there is a same number then add to count

int j;

for(i=0;i<6;i++){

                 

for(j=0;j<6;j++){

if(numContainer[i] == theBet[j])

{

count++;

}

}

}


//the prize for how many same numbers

if(count == 6)

{

printf("You Win the 25Million JACKPOT!!\n");

printf("\n"); 

}

else if(count == 5)

{

printf("You Win RS 10000\n");

printf("\n"); 

}

else if(count == 4)

{

printf(" You win RS 4500\n");

printf("\n"); 

}

else if(count == 3)

{

printf("You win RS 100\n");

printf("\n"); 

}

else if(count<3)

{

printf("Sorry,You Lose\n");

printf("\n"); 

}


system("PAUSE");

return(0);

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users