1. ## Random Numbers

hi all,
I am required to generate an array having 100 random numbers, where there is absolutely no repetition and the case where the ith element of the array is not the number " i " itself is not allowed ..
I found some ideas where we could generate pseudo random numbers using the idea:
x(n+1) = (x(n)*P1 + P2) (mod N) .
where x(n) is the nth element,
P1 and P2 are constants
N is also a constant.. the value of x0 must be chosen appropriately..
This does not guarantee absolute randomness..
Can some one help me with this please...

2. I would just use something simple like rand() to generate the random numbers. Then just check if the number is already in the array and that it's not the same as the array index you're storing it in. If one of those checks fails just grab another random number and try again.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int nums[100];
int i, j;
int again;

srand(time(0));  // Seed pseudo random number generator

for(i = 0;i < 100;++i)
{
do
{
again = 0;

nums[i] = rand();

if(nums[i] == i)
{
again = 1;
continue;
}

for(j = 0;j < i;++j)
if(nums[j] == nums[i])
{
again = 1;
break;
}
} while(again);
}

for(i = 0;i < 100;++i)
printf("nums[%d] = %d\n", i, nums[i]);

return 0;
}```

3. Since rand() returns a number from 0 to RAND_MAX (which is <= INT_MAX, and is on two of my compilers, 2^16-1 and 2^32-1), it might take a long time to get the value you're looking for. You should limit the value (but not with modulus): http://eternallyconfuzzled.com/articles/rand.html.

4. I didn't see in his post that he was looking for specific values. Since the only qualification is that none of the numbers can match (or equal the array index) then I think the largest range possible would be best.

5. rand() does not guarantee absolute randomness either. It uses a simple linear congruential algorithm. Go use some radioactive kitten litter for some real random values.

You could use a primitive root modulo a prime to loop through a set of distinct values. Repetitions are mathematically impossible. Or set the array values using indexes from a randomly generated hash table.

6. Linux also provides /dev/random and /dev/urandom. It uses random events such as keyboard strokes and harddrive activity to generate entropy data which you can use for more-than-pseudo-random-numbers if you really need that (and your OS supports it).

http://www.die.net/doc/linux/man/man4/random.4.html

7. > I am required to generate an array having 100 random numbers, where there is absolutely
> no repetition and the case where the ith element of the array is not the number " i " itself is
> not allowed ..
This hardly describes any definition of random.

This seems to fit your rules, but is decidedly non-random
Code:
`for ( i = 0 ; i < 100 ; i++ ) arr[i] = i + 1;`
No repetition, and no arr[i] contains i

Perhaps you need to say why you need an array which matches these properties you list.

8. This hardly describes any definition of random.
To be fair, the requirements on no repetition and "ith element of the array is not the number " i " itself" can be seen as additional requirements on top on the 'randomness'.

9. ## clarification

hi,
Apparently, the problem was not very clear.
I wish to state the problem again:

"I want an array of size 100 filled with random numbers.
The range of numbers is between 0 and 99.
On top of this, there should not be any repeated numbers and
the number in the ith position of the array must not be the number "i" itself." I am sorry for the trouble, if any.
No genuine requirement as such, this problem came up when a bunch of us were discussing random numbers in general
thanks,

10. Fill the array from 0 to 99.
Shuffle it as long as there are any elements whose value matches its index.

Quzah.

11. Shuffle it as long as there are any elements whose value matches its index.
What if the process never completes? It might happen. In any case, that is a helluva inefficient operation, in theory.

12. Well, after shuffling check for elements whose value matches the index. Shuffle those elements, or simply swap them around.

13. Originally Posted by jafet
What if the process never completes? It might happen. In any case, that is a helluva inefficient operation, in theory.
Maybe the way you write it makes it inefficient. It depends on how you shuffle. Then again, I don't shuffle like a moron.

Besides, the OP said nothing of efficiency.

Quzah.

14. Originally Posted by ME
in theory
Anyway, random_shuffle is a quick fix for the OP's problem, and should work in most* practical situations.

* "most" == "inconcievably huge proportion of".

I should stop thinking like a math geek

15. Anyway, random_shuffle is a quick fix for the OP's problem,
I agree, though there's still the check for elements whose value matches the index. I dont think std::random_shuffle has any guarantees on that.

and should work in most* practical situations.
* "most" == "inconcievably huge proportion of".
Unfortunately, this is the C forum, so a #include <algorithm> followed by the use of std::random_shuffle may not be practical.