Here's the excellent code from john.c, but using the parameters for the problem (tickets with 6 numbers from 1 to 49) and a "trully random number generator", if your processor is Intel or AMD and supports it:
Code:
// tickets.c
//
// Compile with:
// cc -O2 -march=native -o tickets tickets.c
//
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
// This will work for GCC and Intel/AMD processors only.
#if defined(__x86_64) || defined(__i386)
#ifdef __GNUC__
#include <immintrin.h>
#endif
// immintrin.h defines __RDRND__ if the target processor supports it.
#ifdef __RDRND__
// Need __cpuid() in main().
#include <cpuid.h>
static unsigned int RAND_( void )
{
unsigned int r;
while ( ! _rdrand32_step( &r ) );
return r;
}
static void do_nothing(void) {}
#endif
#endif
static void SRAND_(void) { srand(time(NULL)); }
static unsigned int (*RAND)(void) = (unsigned int (*)(void))rand;
static void (*SRAND)(void) = SRAND_;
// HIGH_NUM cannot exceed 63!
#define HIGH_NUM 49
#define TICKET_SIZE 6
#define NUM_TICKETS 20
uint64_t generate_ticket( unsigned int count )
{
uint64_t mask, ticket = 0;
while ( count-- )
{
do
mask = 1ULL << ( RAND() % HIGH_NUM );
while ( ticket & mask );
ticket |= mask;
}
return ticket;
}
void print_ticket( uint64_t ticket )
{
unsigned int count, i;
for ( i = 0, count = HIGH_NUM; count--; i++ )
if ( ticket & ( 1ULL << i ) )
printf( "%2u ", i + 1 );
putchar( '\n' );
}
void print_highlighted_matches( uint64_t ticket, uint64_t draw )
{
uint64_t mask, matches = ticket & draw;
unsigned int i, cnt = 0;
for ( i = 0; i < HIGH_NUM; ++i )
{
mask = 1ULL << i;
if ( ticket & mask )
{
if ( matches & mask )
{
printf( "\033[1;36m" );
++cnt;
}
printf( "%2u ", i + 1 );
if ( matches & mask )
printf( "\033[m" );
}
}
printf( " [ %u ]\n", cnt );
}
int main( void )
{
unsigned int i;
uint64_t draw, tickets[NUM_TICKETS];
// Test if your processor supports RDRAND only
// if __RDRND__ is defined.
#ifdef __RDRND__
int a, b, c, d;
__cpuid( 1, a, b, c, d );
if ( c & bit_RDRND )
{
RAND = RAND_;
SRAND = do_nothing;
}
#endif
SRAND();
for ( i = 0; i < NUM_TICKETS; ++i )
tickets[i] = generate_ticket( TICKET_SIZE );
draw = generate_ticket( TICKET_SIZE );
printf( "Draw:\n" );
print_ticket( draw );
putchar( '\n' );
printf( "Tickets:\n" );
for ( i = 0; i < NUM_TICKETS; ++i )
print_highlighted_matches( tickets[i], draw );
return 0;
}