# Thread: problem with random sorting

1. ## problem with random sorting

I am doing an assignment where I am trying to get random numbers between 1-10.
But I dont want any number to come more than one time. I am using the following code.

Code:
```#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include <time.h>
void randomsort ();

int main ()

{
clrscr();
randomsort();
getchar();
return 0;

}

void randomsort()

{

int b[10],i;
time_t t;
srand ((unsigned) time(&t));
for(i=1;i<=10;i++)
{
b[i]= rand() % 11;
if (b[i]==b[i])
printf ("%d\n",b[i]);
}
}```
It chooses random numbers but the output is like
0
2
0
2
2
1
1
9
9
It is giving me the number more than ones.

2. Fill the array with the integers from 1 to 10. Shuffle the array, and you're done.

To shuffle the array, first select at random one of the array elements, then swap it with the first element. Then, consider the subarray consisting of the array elements other than the first element. Select at random one of the elements from this subarray, then swap it with the first element of this subarray. Consider the subarray consisting of the subarray's elements other than the first... repeat until the subarray is one size 1.

3. You've got a couple other issues here. . . index loops should go from 0 to < len (not 1 < len + 1). Also, if you want random numbers from 1 - 10, you should mod your rand() at 10 (giving you 0 - 9) then add one.

As for your issue of random w/out replacement, I'd suggest you assign a tmp variable and, each time you get a new number, make sure it is not already in the list. OR, a more simple way to handle this is to create an array of int with each value being 1 .. 10, then mix up the list by having the random number generated be the new index of the value. This would, I think, properly mix up the data.

EDIT: Crap, too slow on the type this morning. . . I must not be using "proper touch type" methods.

4. Originally Posted by Kennedy
You've got a couple other issues here. . . index loops should go from 0 to < len (not 1 < len + 1). Also, if you want random numbers from 1 - 10, you should mod your rand() at 10 (giving you 0 - 9) then add one.

As for your issue of random w/out replacement, I'd suggest you assign a tmp variable and, each time you get a new number, make sure it is not already in the list. OR, a more simple way to handle this is to create an array of int with each value being 1 .. 10, then mix up the list by having the random number generated be the new index of the value. This would, I think, properly mix up the data.

EDIT: Crap, too slow on the type this morning. . . I must not be using "proper touch type" methods.

Another way do accomplish this, as I did something somewhat similar is to just populate an array element with '1' only if that random number(the index of the array) was hit. In other words use the index of the array as your deciding factor, have the complete array initialized to all 0's then use the random number you get by your generator as the index, then check its value, if set to 0, then change it to one, if there is a 1 already present then ignore and try again. Hope that makes sense.

5. Originally Posted by slingerland3g
Another way do accomplish this, as I did something somewhat similar is to just populate an array element with '1' only if that random number(the index of the array) was hit. In other words use the index of the array as your deciding factor, have the complete array initialized to all 0's then use the random number you get by your generator as the index, then check its value, if set to 0, then change it to one, if there is a 1 already present then ignore and try again.
In this case, since it appears that Exorcist wants an array whose size is as large as the range of possible numbers, that may not be as efficient as normal shuffling since the chance of producing a duplicate would be high towards the end. (But of course, this kind of efficiency does not matter when you only have ten elements )

6. Thanx. I have managed to solve the problem. Thanx a lot for your help everyone