random NUmbers
-
Ok, here's the deal. I got an "array" of 60 normal variables and a separate "array" of 10 filler (fake) variables I need to go through the "array" of 60 and as I do I need to randomly place all the variables of the "array" of 10 between some of those of the first array if I just do something like if(rand()%(60/10)) use normal; else use filler; I might not get all ten variable in. What algorythm should I use to randomly run each of the 10 variables between the 60 normal ones, and to make sure I do not get a systematic large chunk of normal variables? P.S. I don't know if what I said is very clear, sorry about that....
-
Ok, here's the deal. I got an "array" of 60 normal variables and a separate "array" of 10 filler (fake) variables I need to go through the "array" of 60 and as I do I need to randomly place all the variables of the "array" of 10 between some of those of the first array if I just do something like if(rand()%(60/10)) use normal; else use filler; I might not get all ten variable in. What algorythm should I use to randomly run each of the 10 variables between the 60 normal ones, and to make sure I do not get a systematic large chunk of normal variables? P.S. I don't know if what I said is very clear, sorry about that....
Something like this should work:
int fillers_used=0;
for(int i=n;i<60;++i){
if(fillers_used<10 && rand()%((60-i)/(10-fillers_used))) {
//use normal
}
else{
// use filler
++fillers_used;
}
}Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Something like this should work:
int fillers_used=0;
for(int i=n;i<60;++i){
if(fillers_used<10 && rand()%((60-i)/(10-fillers_used))) {
//use normal
}
else{
// use filler
++fillers_used;
}
}Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Thanks a lot... but this way wouldn't the frequency of the filler used be increased as I go through the array? (nnnnnnnnfnnnnnnnfnnnnnfnnnnfnnfnnff)?
Well there's a deficiency due to truncation problems, I guess you should use something like this instead:
static double rand01()
{
int r;
while((r=rand())==RAND_MAX);
return (double)rand/RAND_MAX;
}
...
if(rand01()<(double)(10-fillers_used)/(60-i)){
...Apart from this, the process is unbiased. To prove this, we can use a little probability. Let's call Pn the probability that the process uses a filler value at position n. Also, let p=10/60, q=1-(10/60).Then: _P_0 = 10/60 _P_1 = _P_0·(9/59)+(1-_P_0)·(10/59) = (10/60)·(9/59)+(50/60)·(10/59) = 590/3540 = 10/60 P__n = p__n·((10-n)/(60-n)) + p__n-1·q·((10-(n-1))/(60-n)) + ··· + q__n·(10/(60-n)) It can be formally proven that P__n = 10/60 for every n, but also a small test program doing the calculations will convince you. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo