Thread: Technique/algorithm for writing a lottery app

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    29

    Technique/algorithm for writing a lottery app

    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
    fseek ( randomnumber*sizeof(int),SEEK_SET);
    .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(maximumnumber*sizeof(int),SEEK_SET)
    in the list and then decreases the maximumnumber variable by 1.

    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

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Sorry,I wrote a wrong thing.I said if the user wants to create all combinations of 76 numbers,it is wrong.The true version is,if the user wants to create a lot of combinations,for example more than 1.000.000.000 combinations.
    Last edited by Awareness; 03-22-2015 at 02:22 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Look up "permutations and combination".

    For extra fun, add "c++" to the end of the search.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    29
    Thanks for your answer.I will look at them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TGA RLE compressing ( writing ) algorithm on c++
    By Bloupies in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2014, 11:00 AM
  2. Writing a Delete Algorithm in an ordered linked list
    By coolguy67 in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2013, 09:27 AM
  3. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  4. a more advanced technique
    By Yarin in forum C++ Programming
    Replies: 53
    Last Post: 08-07-2008, 04:57 AM
  5. Compression Technique
    By vikranth in forum C Programming
    Replies: 9
    Last Post: 11-06-2007, 09:43 AM