# non-repeating random numbers

• 10-29-2006
redx2evil
non-repeating random numbers
i'm supposed to generate 7 random numbers whereby each number can be any number between 1 to 45. but i need to ensure that there're no duplicates in those 7 numbers

Code:

#include <stdio.h>

int main(void)
{
int count, i=1, r_num[7];

srand (time(0)); /* seed is determined at run-time by the system */

for(count=0; count<7; count++)
r_num[count] = rand()%45 + 1;

for(i=0; i<7; i++)
{
for(a=0, a<7; a++)
{
if(i == a)
continue;
if(r_num[i] == r_num[a])
r_num[a] = rand()%45 + 1;
}
}

these are my codes..but im stuck..can anyone help me?
• 10-29-2006
vart
There are at least 2 ways to achieve the goal:

1.
Code:

Step 1 Generate a number a1 from  1 to 45
Step 2 Generate a number a2 from 1 to 44
If (a2 >= a1) a2++;
Step 3 etc

2.
Code:

Step 1 Generate a number a1 from 1 to 45
Step 2 Generate a number a2 from 1 to 45
If (a2 == a1) goto Step2
Step 3 etc

• 10-29-2006
Prelude
There are two obvious solutions. Since your range is small, an efficient way would be to fill an array with sequential numbers and then randomly shuffle the array. The first 7 are your numbers:
Code:

int array[45];
int i;

for ( i = 0; i < 45; i++ )
array[i] = i + 1;

shuffle ( array, 45 );

I'd recommend trying to write the shuffle first before asking for help, because it's a good learning experience. The second obvious solution is inefficient, but you probably won't notice it with such a small range. For each number you generate, you check to see if it's already in the array. If it is, start over:
Code:

int i = 0;

while ( i < 7 ) {
int r = rand() % 45 + 1;
int j;

/* Search the existing numbers */
for ( j = 0; j < i; j++ ) {
if ( array[j] == r )
break;
}

/* If we broke early, don't do anything */
if ( j == i )
array[i++] = r;
}

• 10-29-2006
redx2evil
actually im still quite new to C. so im not really sure what does shuffle function do. mind to explain?
and im supposed to print out the 7 numbers. so how do i get them out of the 'shuffled array'?
• 10-29-2006
vart
do you use winamp? what does the shuffle function do there?

you reorder you array in the ramdom order - then take 7 first numbers from it...
• 10-29-2006
redx2evil
ya i do know what shuffle means. but is the function shuffle in the standard library or sumthing?
• 10-29-2006
vart
Quote:

Originally Posted by redx2evil
ya i do know what shuffle means. but is the function shuffle in the standard library or sumthing?

Quote:

Originally Posted by Prelude
I'd recommend trying to write the shuffle first

Nope. Don't think so. It's all upto you
• 10-29-2006
redx2evil
ok thanks :)
• 10-29-2006
anon
It is in C++ :)

But a simple way to do it yourself goes like that:
Code:

step 1:
pick random number from 0 - (size-1);
swap array[0] and array[picked_number];

step 2:
pick random number from 1 - (size-1);
swap array[1] and array[picked_number];

...

step (size-1)
pick a random number from (size-2) - (size-1);
swap array[size-2] and array[picked_number];

• 10-29-2006
redx2evil
oh ya..thanks a lot prelude :)
• 10-29-2006
quzah
Me too, me too!
Code:

int array[45] = {0}, x = 0;
while( x < 7 )
{
if( array[ rand() % 45 ]++ == 0 )
x++;
}

/* print non-zero element's index */

Same song, second verse:
Code:

int array[ 45 ] = {0}, x = 0, y = 0;

for( x = 0; x < 45; x++ )
array[ x ] = x + 1;

for( x = 0; x < 7;  )
{
y = rand() % 45;
if( array[ y ] )
{
array[ y ] = 0;
x++;
}
}

/* print zero element's index */

Quzah.