-
UK Lotto Generator
hi, This is a small lotto generator I made it simple it makes 59 numbers stores them into a array and then suffle the array and then I pick the first 6 numbers. There is more than likey a better way of doing this. But it seems to work. Hope you like it comments welcome.
Code:
// File : lotto.c
// By : Ben a.k.a DreamVB
// Date : 17/06/2020
// Info : A small UK lotto number generator.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#define MAX_BALLS 59
#define WIN_BALLS 6
void shuffle_array(int arr[], int size)
{
int t = 0;
//Get random seed
srand(time(NULL));
int i = size - 1;
while(i > 0)
{
const int j = rand() % (i + 1);
//Swap items in array
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
//DEC i
i--;
}
}
int main()
{
int balls[MAX_BALLS] = {0};
int x = 0;
//Load balls
for(int x = 0; x<MAX_BALLS; x++){
balls[x] = x+1;
}
//Shuffle the 59 balls
shuffle_array(balls,MAX_BALLS);
//Display lotto banner
puts("+------------------------+");
puts("| UK Lotto Winning Balls |");
puts("+------------------------+");
x = 0;
//Print out the first 6 winning balls
while(x < WIN_BALLS){
printf("%d ",balls[x]);
x++;
}
puts("\n--------------------------");
return 0;
}
-
Re: UK Lotto Generator
The 2 while loops should be for loops. You then don't need i and x defined outside of the loop.
-
Re: UK Lotto Generator
For an 'easier' C++ consider:
Code:
#include <algorithm>
#include <numeric>
#include <array>
#include <iterator>
#include <random>
#include <iostream>
constexpr size_t max_balls {59};
constexpr size_t win_balls {6};
int main()
{
std::array<size_t, max_balls> balls;
std::iota(std::begin(balls), std::end(balls), 1);
std::shuffle(std::begin(balls), std::end(balls), std::mt19937(std::random_device {}()));
std::copy_n(std::begin(balls), win_balls, std::ostream_iterator<size_t>(std::cout, " "));
}