I am not sure if my question is suitable for this forum section or C Programming section.I am trying to write a lottery program.The problem is, the 5 numbers of any combination shouldn't repeat the same combination.It's easy to have random numbers,so then I save the combinations to memory or a file and then when it creates a new combination, the program checks if the same combination was taken before.If it was taken,then it creates a new combination.
Let's say this is a pick 5 type lottery.The program creates combinations of 5 numbers.And let's say the maximum number is 80 and so let's say the user wanted the program to create all the combinations of 76 numbers.
Now the program writes to memory or a file every combnation it creates to check if the new combination was taken before. According to my calculations,the total number of combinations are 1311713640.
Multiply this number with 5 because there are 5 numbers in each combination,it is 6558568200 and multiply this with 4 because 1 int is 4 bytes(in 32 bit systems I think),this is 26234272800. So I think if the program writes combinations to a file,it wil reach 26.234.272.800 byte = a bit more than 26 gb 234mb.
So it would be a very big file,this is one of my problems.The other one is,checking the combinations and if one of them matches an earlier taken one then creating a new combination will be very long process in time.Because as the number of possible new combinations decrease, creating a new random combination which wasn't taken before will be much more difficult,will take much more time,may even seems like there is an infinite loop and the program halted.
I saw on internet a good method , program creates combinations from random numbers.For example,the random number is 51,and the program creates a 5 number combination from this number.In this way, you have to write to memory or a file just one number for each combination,so now ,according to my calculations, to find every possible combination of 76 numbers, you need to write 5.246.854.560 bytes = 5 gb 246 mb.
It's still a big file although it is much less than writing 5 numbers for each combinations to a file.But also,the problem of "as the number of possible new combinations decrease, creating a new random combination which wasn't taken before will be much more difficult,will take much more time,may even seems like there is an infinite loop and the program halted." still continues.Also,let's say there will be a power ball,I mean 5+1,and let's say maximum powerball number is 40,then you have to multiply 5.246.854.560 bytes which will be written to file,with 40.
For solving the "as the number of possible new combinations decrease ..." problem,I thought about making a list of numbers in a file,this list will start from 1 and last with the number of all possible combinations.There will be a maximumnumber variable,it's first value is the number of all possible combinations. Using the technique of creating a combination from a random number, when the program creates a random number, I use fseek and it will go forward in the file like.And then program reads the number in the list in the file,then saves the number into a variable(for example rnumber variable), then it replaces the number in the list with the maximum number's value (fseek ( randomnumber*sizeof(int),SEEK_SET);in the list and then decreases the maximumnumber variable by 1.fseek(maximumnumber*sizeof(int),SEEK_SET)
Then program will create a random number from the value of rnumber variable.This solves the speed problem maybe but can't solve creating a very big size of file problem.
So I couldn't find a good solution