-
Ask about random number
I want to generate random number between 1- 400,000 which is not the same value.
Example.
If X1 = 254899
X2 not have the same value as X1.
and X3 not have the same value as X1 and X2 .
I want to generate data X1 - X400,000 which not have the same value and each data have value between 1-400,000
If you know how to do that, please tell me. Thank you.
-
Random numbers, then just compare the new rand number with what you've already got. If its the same, get another random number.
-
How long do you reckon that method would be good for Hammer? If you wanted a number between 0 and 100 but no duplicates, when does it take to long to reach the next number because you're throwing away duplicates?
-
>How long do you reckon that method would be good for Hammer?
Yeah, I suppose I didn't read the original question fully :rolleyes: In answer to your question, not long, I suppose. My theory is good in the short term only. There must be a better way, but it's not coming to me at present... :o
-
Depends on the situation really. If you want 100 different numbers between 1 and 1000000, then it'll be fine. But if you want 80 random numbers between 1 and 100 a better method would need be to eliminate the 20 random numbers your not going to use. Maybe...
-
I learned this with a program I wrote that shuffled cards.
Code:
int array[400000];
for (int i=1; i<=400000; i++)
{
array[i]=i; // first set each number equal to itself
}
for (int i=1; i<=400000; i++)
{
int x=rand()%400000+1; // pick a random number between 1-400000
// now switch this variable with the variable at the random number generated
int temp=array[x];
array[x]=array[i];
array[i]=temp;
}
This will randomly place the numbers 1-400000 in to 400000 variables without duplicates
-
on computers its unfortuneate but some things have limits. One of those is the maximum number an int can hold.Also in the example shown RAND_MAX has surely been exceeded too. remember that rand() can only produce values up to RAND_MAX which on my system is 0x7fff