cancel
Showing results for 
Search instead for 
Did you mean: 

Lottery Number Generator

Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Lottery Number Generator

Following on from this thread I thought it would be fun to waste some of my day (last day of holidays) to write a ‘random’ number generator for your lottery numbers.

Rather than supply a link to an executable I thought it easier to provide the source code so you can easily build it on yourself on your PC.

Source Code:

int main(int argc, char * argv[])
{
	std::random_device rd ; 
	std::mt19937 engine{ rd() } ; 
	std::vector< unsigned > machine ;
	std::list< unsigned > results ;
	// See if a number's been passed on the CLI
	int times = argc == 2 ? std::min(std::stoi(argv[1]), 7) : 1 ;
	// Randomise the values in the vector
	for (int n(0); n < times; n++) {
		std::srand ( unsigned ( std::time(0) ) ) ;
		// Fill vector with values in range
		for (int i= 1; i < 59; ++i) machine.push_back(i) ;
		// Randomise the content of the vector
		std::random_shuffle (machine.begin(), machine.end(), [](unsigned i) { return std::rand() % i ;} ) ;
		// Start the output
		std::cout << "Line # " << std::setw(2) << (n + 1) << " : " ;
		// Draw the numbers needed
		for (int i(0); i < 6; i++) {
			// Get a randomly generated int within the range of the vector size
			std::uniform_int_distribution< int > dist(0, machine.size() -1 ) ;
			int pos = dist(engine) ;
			// Push the selected value into the list
			results.push_back(machine[pos]) ;
			// Remove the value from the vector
			machine.erase(machine.begin() + pos) ;
		}
		machine.clear() ;
		// Sort the selection in ascending order
		results.sort() ;
		// Write the results to the console
		for (std::list< unsigned >::const_iterator ci = results.begin(); ci != results.end(); ++ci) {
			std::cout << std::setw(2) << (*ci) << " " ;
		}
		std::cout << std::endl << std::flush ;
		results.clear() ;
		// Remove the comment from the line below if you want a delay between lines
		//std::this_thread::sleep_for(std::chrono::milliseconds(50)) ;
	}
	std::cout << "Press Enter or Return to Exit" ;
	std::cin.get();
	return 0 ;
}

Mac / Linux Makefile:

CC = g++
CFLAGS = -Wall -O2 -std=c++14 -DNDEBUG

TARGET = lotto
INC = ./
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) $(OBJECTS) -o $(TARGET) 

main.o: main.cpp 
	$(CC) $(CFLAGS) -c main.cpp -I$(INC) 

The make file is for Mac or Linux and will just work assuming you have build essentials installed. For Windows I’m afraid you’d need to create your own makefile or Visual Studio project. But if you do please feel free to post it for others.

 Edit:- Add expected output:

iMac:Lottery Mook$ ./lotto 7
Line #  1 :  3  7 15 22 30 34 
Line #  2 : 21 26 30 43 56 58 
Line #  3 :  9 19 27 43 47 58 
Line #  4 :  1  7 12 24 51 53 
Line #  5 : 10 19 21 36 47 52 
Line #  6 :  8 30 40 42 48 58 
Line #  7 : 19 22 27 33 40 47 
Press Enter or Return to Exit
22 REPLIES
Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Re: Lottery Number Generator

Here's a version for the Python enthusiast:

Source Code:

#!/usr/bin/python
import random


def main():
        for d in range(1, 7):
                machine = list(range(1, 59))
                results = list()
                random.shuffle(machine)
                sRand = random.SystemRandom()
                print("Line # %d : " % d, ' ', end='')
                for i in range(0, 6):
                        choice = sRand.randrange(1, len(machine))
                        results.append(choice)
                        del machine[choice]
                results.sort()
                for x in results:
                        print ('%-2s'% x, ' ', end='')
                print('')

if __name__ == "__main__": main()

Load in to IDLE and execute and you'll get output just like the C++ version above :

Line # 1 :   2   9   19  28  48  50  
Line # 2 :   0   6   32  39  43  52  
Line # 3 :   6   10  10  11  35  50  
Line # 4 :   3   11  24  40  43  44  
Line # 5 :   1   6   24  27  40  41  
Line # 6 :   15  22  23  26  29  50  
Line # 7 :   1   25  25  33  44  54 

However, I can see a noticeable difference in execution speed with the C++ being much faster.

1st Edit:- Spotted an error can you see it?

2nd Edit:- Error fixed.

 

Browni
Seasoned Champion
Posts: 1,519
Thanks: 429
Fixes: 35
Registered: 02-03-2016

Re: Lottery Number Generator

Line #3 has the number 10 twice!
I must have been really bad in a previous life. This is my 3rd ISP in a row that uses lithium.
Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Re: Lottery Number Generator

Well spotted @Browni, and you'll also have noticed that the numbers selected have 0's in them.

But that's easily fixed by using:

choice = sRand.randrange(1, len(machine))

Also note that this script needs Python3

Moderator
Moderator
Posts: 25,760
Thanks: 1,120
Fixes: 47
Registered: 14-04-2007

Re: Lottery Number Generator

Years ago I wrote a Lottery Number Generator in BBC Basic.

I painstakingly keyed in all the past results into the database then each week after giving it the winning numbers I asked it for next weekend's winning numbers.

They came up winner once only but I hadn't bought a ticket.

As an aside it gave me some stats like the top 20 all time balls out of the machine.

At the time 41 was the most popular number drawn.

Customer and Forum Moderator.
Product of the Tyrell Corporation
Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Re: Lottery Number Generator

Sounds like fun @Strat but surely probabilistic forcasting is wasted when the system is truly random.

Moderator
Moderator
Posts: 25,760
Thanks: 1,120
Fixes: 47
Registered: 14-04-2007

Re: Lottery Number Generator

Is the RND function truly random.

It was fun and extended to a great many lines of code.

Customer and Forum Moderator.
Product of the Tyrell Corporation
Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Re: Lottery Number Generator

No it's not, as I understand it you need hardware to get true random numbers, but my reference was to the Lottery itself being truly random and not any pseudo random code like that above.

Community Veteran
Posts: 13,921
Thanks: 514
Fixes: 7
Registered: 01-08-2007

Re: Lottery Number Generator


Strat wrote:

I painstakingly keyed in all the past results into the database then each week after giving it the winning numbers I asked it for next weekend's winning numbers.

 


I seem to remember when Derren Brown predicted them correctly on TV he had also used historical results to help him and his volunteers pick the correct numbers.

I need a new signature... i'm bored of the old one!
Community Veteran
Posts: 4,757
Thanks: 1,048
Fixes: 27
Registered: 16-10-2014

Re: Lottery Number Generator

Derren Brown is an illusionist so by definition was his lottery trick, done as I understand it using a split screen.

bjallenby
Aspiring Pro
Posts: 178
Thanks: 76
Registered: 13-05-2017

Re: Lottery Number Generator


Mook wrote:

No it's not, as I understand it you need hardware to get true random numbers,


Unfortunately there was the whole "don't trust hardware RNGs" with Intel's RdRand a few years back and the Snowden releases. Relying strictly on hardware (particularly from Intel) for random number generation is a no-no. Theodore Ts'o who implemented RNG in kernel space wrote

I am so glad I resisted pressure from Intel engineers to let /dev/random rely only on the RDRAND instruction... Relying solely on the hardware random number generator which is using an implementation sealed inside a chip which is impossible to audit is a BAD idea.

RDRAND is of course still used, but it's one of many inputs of randomisation.

The reason that China developed the Loongson processor was so they had an alternative to American based hardware which can't be trusted for security critical infrastructure (like the generation of secure keys). The Russians did the same with Elbrus.

Minivanman
Hero
Posts: 4,794
Thanks: 1,046
Fixes: 1
Registered: 04-11-2014

Re: Lottery Number Generator

All clever stuff and way above my head, but can digital be random?

Seems like a contradiction to me.

 

All views expressed are my own but you can express them too if you want to be right about everything like I am.
bjallenby
Aspiring Pro
Posts: 178
Thanks: 76
Registered: 13-05-2017

Re: Lottery Number Generator

That's a philosophical question. The best we can achieve is pseudo-random generation. Even with hardware. Some argue that quantum mechanics will provide truly random numbers.

I don't think anything is truly random. Everything we know and understand within the universe we exist, is being understood via mathematics. Chaos theory is the branch of mathematics that studies complex systems that appear to be without order, yet finds patterns and repetition within them, which would indicate that there is some form of order.

Community Veteran
Posts: 5,338
Thanks: 608
Registered: 23-09-2010

Re: Lottery Number Generator

That reminded me of the simple program I wrote to pick my pools coupon numbers.

That was way back with a Commodore 64 and the basic cartridge plugged in.

Of course I stopped using it after a couple of goes as all the fun and "skill" was in picking the numbers yourself.

Jonpe
Seasoned Pro
Posts: 1,268
Thanks: 337
Fixes: 2
Registered: 05-09-2016

Re: Lottery Number Generator

I posted this link in the other Lotto-related thread http://www.lottery.merseyworld.com/ It has a lot of stats and is updated as soon as the results are known.

@billnotben  I bought a few Football Pools programs for my ZX Spectrum back in the 1980s and one was rather good at picking 'definite' home wins.  Zetters Pools had what they called Super Homes or something similar (home wins with a goal difference of three or more) and it was possible to win small amounts regularly.  The worst part was to feed in all the required information and wait for the computer to analyse it.