Lottery Generator

+4 Isa Usmanov · November 15, 2014
Hello Everyone!

I have a task:
Lottery number generator. In the Finnish Lotto you need to pick 7 numbers 
out of 39. Write a function, which picks a random number from 1 to 39. 
After that write a program, which generates a random set of Lotto numbers 
(a Lotto ticket). Notice that the same number cannot appear in the set 
twice.
 
Hint: declare an integer array containing 40 elements and initilize all 
its values to zero. After picking a random number, test if the element 
value in that index is zero. If it is, accept the number and set the value 
in that index to 1. Otherwise discard the number and pick a new one. 
After you have successfully picked 7 numbers, you Lotto ticket is ready. 
Finally iterate through the array and print the indexes in which the 
element value is 1.



And here is my code:


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

int myRand();
int main()
{
 srand(time(NULL));
 int checkArr[40] = {};
 int counter;
 int counter2;
 int counter3;
 int sumOfones = 0;
 
  for (counter = 0;counter < 7; counter++){
                if (checkArr[myRand()] == 0){
                checkArr[myRand()] = 1;
                }
                else if (checkArr[myRand()] == 1)
                checkArr[myRand()] = 1;   
                }



for (counter2 = 0; counter2 < 39; counter2++){
         if (checkArr[counter2] == 1)
           //     sumOfones = sumOfones + checkArr[counter2];
                printf("%d  ", counter2+1);
         }


printf("\n\n");
for(counter3 = 0; counter3 < 39; counter3++){
             printf("%d", checkArr[counter3]);    //  For testing purpose
              }


    


    system("PAUSE");    
    return 0;
}

int myRand(){
     
     int numR = (rand() % 38) + 1;    
 }









/* My problem is that sometimes program shows 5 or 6 numbers insted of 7. 
It's because of random number sometimes gives same number twice.
Do you know how to make FOR loop give 7 different numbers? (Otherwise discard the number 

and pick a new one.(q)) 

I hope for your help friends. I'm stucked */

Post a Reply

Replies

Oldest  Newest  Rating
-1 Isa Usmanov · November 16, 2014
That is what I exactly mean.
What kind of action I should implement here:


for (counter = 0;counter < 7; counter++){
if (checkArr[myRand()] == 0){
checkArr[myRand()] = 1;
}
else if (checkArr[myRand()] == 1)  
checkArr[myRand()] = 1;        //     <========== HERE
}
 

To discard the number (if it is repeated ) and pick a new one.
I tried to minus one counter if the value is repeated, but I doesn't work. 
0 Brandon Elliott · November 17, 2014
i think you want something more like this


for (counter = 0;counter < 7; counter++){
int randomNum = myRand(); //store a new random number
while(checkArr[randomNum] == 1){
randomNum = myRand(); //keeps generating a new random number until it gets an unpicked one
}
checkArr[randomNum] = 1; //stores the random number in the array
0 Isa Usmanov · November 17, 2014
wonder time, Thank you a lot. It was helpful, now it works. 

Brandon Elliott thank you too for reply. It seems to work also.
0 Robin Stenberg · November 26, 2014
Hi! I have a similar task but with only between 1-36 digits. And the user chooses how many lotteryr rows it wants. 

My problem is that I cant get the numbers to appear in different rows. In other words. I want 7 didigts on each row. 
I'm sure that it is a simple fix, but I'm a total beginner at C and I would appreciate some help.

Can't I just write an if-statement that breaks the digits?

Heres my code:



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

#include <time.h>

int myRand();
int main()

{

int i;

srand(time(NULL));

int checkArr[37] = {};

int counter;

int counter2;

int counter3;

int sumOfones = 0;


printf("How many rows do you want?\n");

scanf("%d", &i);

printf("\n");

printf("Here is your rows \n ",i);

printf("\n");

int j =0;

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

{

for (counter = 0;counter < 7; counter++){
int randomNum = myRand(); //store a new random number
while(checkArr[randomNum] == 1){

randomNum = myRand(); //keeps generating a new random number until it gets an unpicked one


}

checkArr[randomNum] = 1;

}

}

for (counter2 = 0; counter2 < 36; counter2++){
if (checkArr[counter2] == 1)

// sumOfones = sumOfones + checkArr[counter2];




printf("%d ", counter2+1);

if (checkArr[counter2]>7)

{

    printf("\n");

}
}


printf("\n\n");
system("PAUSE");
return 0;

}
int myRand()

{
int numR = (rand() % 35) + 1;
}
  • 1

C

106,970 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator