# Thread: Random Lotto number generator(no duplicates)

1. ## 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

2. 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. 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.

4. 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. 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. 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
}```