# Unique random numbers

• 11-23-2004
aydin
Unique random numbers
ok i read the FAQ on random numbers but couldnt make much sense of it, i am writing code for an assigments, which is to create a lottery program, here is the code i have so far:
Code:

``` #include "stdafx.h" #include "time.h" int lottery[6]; void main() {         srand((unsigned)time(NULL));         lottery[0] = rand()%49;         lottery[1] = rand()%49;         lottery[2] = rand()%49;         lottery[3] = rand()%49;         lottery[4] = rand()%49;         lottery[5] = rand()%49;         printf("%d\t%d\t%d\t%d\t%d\t%d\n\n", lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], lottery[5]);         system("pause"); }```
can anyone help so i can create UNIQUE random numbers?
• 11-23-2004
hk_mp5kpdw
1. Create the first random number, store it in array.
2. Create next random number.
3. Check if number generated exists in array, if not, add to array.
4. If array full, move on, otherwise go back to step 2.
• 11-23-2004
aydin
how do you check if the number is the same in the array :D
• 11-23-2004
hk_mp5kpdw
You're probably going to need to use nested loops. An outer loop which keeps generating numbers until we have inserted X number of unique values, and an inner loop which checks the array from index 0 up to (not including) whatever the current count is at.
• 11-23-2004
Salem
> void main()
int main

Perhaps you should consider say
int array[49];
store 1 to 49 in each respective slot

shuffle the array - randomly exchange pairs of array elements
exchanging disturbs the order, but preserves the uniqueness

take the first six elements of the array as your lottery draw
• 11-23-2004
PING
Quote:

Originally Posted by aydin
how do you check if the number is the same in the array :D

i think that we are ending up confusing the guy,he probably is just starting out with C.i think that what hk_mp5kpdw said is nice enough.try to implement it on your own
• 11-23-2004
xErath
Code:

```#include <stdlib.h> #include <stdio.h> int is_number_in_array(int lottery[],int size, int number){     //cicle the array and compare the number.     // return 1 when match is found (1 means true)     return 0; } int main(){     int lottery[6];     srand((unsigned)time(NULL));     /*************     LOOP1: loop till 6 number have been generated       LOOP2:             generate random number             check if it is in array//use a function             if so continue loop2         place number in array            ******************/     printf("%d\t%d\t%d\t%d\t%d\t%d\n\n", lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], lottery[5]);     system("pause");     return 0; }```
Now the part you wanted (to check if a number is part of the array) you write it.
• 11-23-2004
Thantos
Salem's answer is probably the easist and most effective for this problem.