Peter
Peter Campbell Smith

Lotteries and sequences

Weekly challenge 246 — 4 December 2023

Week 246 - 4 Dec 2023

Task 1

Task — 6 out of 49

6 out of 49 is a German lottery. Write a script that outputs six unique random integers from the range 1 to 49.

Examples


Example 1
Output: 3, 10, 11, 22, 38, 49

Analysis

The obvious solution is to use Perl's rand() function, checking for duplicates, so that's what I did.

Purists will claim, correctly, that these numbers are not strictly random, but true randomness is not something computers are good at.

Many years ago I was responsible for the maintenence contract for ERNIE 4, the device that generated winning numbers for the UK Premium Bond scheme and which had been designed and built by the company I worked for. ERNIE 4 used thermal noise as the source of its true randomness.

I had the occasional call from the operators saying that it had come up with 8 consecutive 8s, so could we please come and fix it, and I had a standard letter I used to explain why it wasn't broken.

Try it 

Try generating your own set of 6 random numbers. Don't blame me if you don't win the prize!

Script


#!/usr/bin/perl

use v5.26;    # The Weekly Challenge - 2023-12-04
use utf8;     # Week 246 task 1 - 6 out of 49
use strict;   # Peter Campbell Smith
use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

six_from_49();
six_from_49();
six_from_49();
six_from_49();
six_from_49();

sub six_from_49 {
    
    my ($count, $ball, @seen, $result);
    
    # initialise
    $result = qq[Output: ];
    $count = 0;
    
    # find 6 unique 'random' numbers
    while ($count < 6) {
        $ball = int(rand(49)) + 1;
        next if $seen[$ball];
        
        # is unique
        $seen[$ball] = 1;
        $count ++;
        $result .= qq[$ball, ]; 
    }
    
    # show result
    say substr($result, 0, -2);
}

Output


Output: 34, 12, 48, 31, 40, 45
Output: 26, 41, 9, 39, 21, 38
Output: 24, 11, 23, 49, 33, 21
Output: 24, 30, 43, 19, 7, 13
Output: 38, 30, 35, 20, 3, 22