Results 1 to 3 of 3

Thread: UK Lotto Generator

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    625

    Post 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;
    }

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    692

    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.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.8.4)

  3. #3
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    692

    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, " "));
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.8.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width