Lottery Number Generator
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- General Chat
- :
- Lottery Number Generator

Lottery Number Generator
31-07-2017 11:59 AM - edited 31-07-2017 12:01 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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

Re: Lottery Number Generator
31-07-2017 3:01 PM - edited 31-07-2017 3:17 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Lottery Number Generator
31-07-2017 3:04 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator

Re: Lottery Number Generator
31-07-2017 3:16 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: Lottery Number Generator
31-07-2017 3:16 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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. Windows 10 Firefox 84.0.2 (64-bit)

Re: Lottery Number Generator
31-07-2017 3:21 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Sounds like fun @Strat but surely probabilistic forcasting is wasted when the system is truly random.
Re: Lottery Number Generator
31-07-2017 3:30 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Is the RND function truly random.
It was fun and extended to a great many lines of code.
Customer and Forum Moderator. Windows 10 Firefox 84.0.2 (64-bit)

Re: Lottery Number Generator
31-07-2017 3:48 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Lottery Number Generator
31-07-2017 4:11 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@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.

Re: Lottery Number Generator
31-07-2017 4:18 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Derren Brown is an illusionist so by definition was his lottery trick, done as I understand it using a split screen.

Re: Lottery Number Generator
31-07-2017 7:45 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@Anonymous 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.
Re: Lottery Number Generator
31-07-2017 8:44 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
All clever stuff and way above my head, but can digital be random?
Seems like a contradiction to me.
Don't limit the friends you haven't met with arguments you'll never have.

Re: Lottery Number Generator
31-07-2017 9:01 PM - edited 31-07-2017 9:02 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Lottery Number Generator
31-07-2017 10:14 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Lottery Number Generator
31-07-2017 10:23 PM - edited 31-07-2017 10:24 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page