Thread: Random Lotto number generator(no duplicates)

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    2

    Post Random Lotto number generator(no duplicates)

    I have been task with making a random number generator for my class assignment, i have got the random number generator to work but i am having trouble with duplicate numbers, i am aware of techniques using bool and void functions but cannot use these i a haven't progressed that far in the course yet. i know what i want it to do but i cant get my head around the code. The code runs but i am still getting duplictaes any help appreciated Random Lotto number generator(no duplicates)-lotto-jpgRandom Lotto number generator(no duplicates)-lotto-jpg
    Last edited by CMajor1; 11-25-2022 at 11:55 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Getting "duplicate" values from N samples doesn't mean they are not random...

    If you flip a coin there are just 2 possible outcomes (head or tail). 3 tosses will get you, necessarily a "duplicate" value (just an example).

    I strongly recommend to not "invent" your own "random number generator" and use one of the "traditional" ones: LCG, Mersene twister, xorshift, etc...

    PS: Don't post images... post code (or fragments of one). Your link doesn't open.

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    2
    Hey flp1969 thanks for the reply, i haven't invented my own number generator i wouldn't be capable of that, i have adapted one for that suits my lotto format. i also understand your coin flip analogy and you are correct but i am trying to avoid duplicate numbers in the same line as shown in the pic below i have duplicate 11's.

    PS: thanks for the tip about the images.Random Lotto number generator(no duplicates)-lotto-console-jpg

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Ok... 2 things: Instead of dealing with short int, use int.

    And, to obtain an value inside a range using rand(), this:

    range * rand() / RAND_MAX

    won't do what you expect.
    Try:
    Code:
    lotto[i] = lowest + range * ( rand() / ( RAND_MAX + 1.0 ) );
    I'll let you figure it out why...

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    And, again... duplicate values aren't uncommon with 'random' values... You can avoid this checking if a number is already chosen.
    If you are generating 6 values with less then 64 values in the range you can use the bits of an uint64_t object to set which ones you choose and choose another one if the bit is set (just an idea)... Or to use an array of 'range' items for the same purpose).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose the first question is whether you're learning C or C++.

    You posted on the C++ forum, and your fuzzy filename is a .cpp file.

    But the code is pure C.



    Your inner loop to check for duplicates needs to be something like
    Code:
    // check the new number with all previous numbers
    for ( j = 0 ; j < i ; j++ ) {
      if ( lotto[i] == lotto[j] ) // it's a duplicate
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Stuck on 1 Number?
    By Dollydaydream in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2013, 08:43 AM
  2. Only getting 0; random number generator
    By scatterbrain in forum C Programming
    Replies: 10
    Last Post: 10-11-2011, 06:24 AM
  3. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  4. Random number generator
    By jbone0881 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 10:20 PM
  5. Random number generator
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 02:23 PM

Tags for this Thread