wirjawan wrote:maybe you want to at least push the generated random number into the vector?
and try to change
[cpp]
while(Z!=0&&I!=0&&M!=0&&L!=0)
[/cpp]
to
[cpp]
while(!(Z==0&&I==0&&M==0&&L==0))
[/cpp]
hope this helps
What do you mean "maybe you want to at least push the generated random number into the vector?" ? I already pushed the generated numbers into the vector!
Yeah, I tried while(!(Z==0&&I==0&&M==0&&L==0)) but there's no difference. I still get the wrong answer. Huhm!
yellow wrote:
What do you mean "maybe you want to at least push the generated random number into the vector?" ? I already pushed the generated numbers into the vector!
Yeah, I tried while(!(Z==0&&I==0&&M==0&&L==0)) but there's no difference. I still get the wrong answer. Huhm!
you pushed the first generated random number but not the second, the third, the fourth, etc...
you are always searching for vector<int> numProduced of size (caseNum).
another problem is that you never clear the numProduced vector. so the data from previous run will still be there.. so on the first run.. you will have 1 randomNum( let's say X), the second run.. you will have 2 randomNum (X and Y) // X is not suppose to be there
1.
[cpp]
numProduced.push_back(randomNum);
int first= randomNum;
int count=1;
while(1)
{
randomNum=(Z*randomNum+I)%M;
if(Search(numProduced,randomNum))
break;
numProduced.push_back(randomNum);
count++;
}
[/cpp]
this will give you a WA iff the number doesn't start at the first place..
ex. the sequence is 2 4 5 6 7 4
your count = 5 (at 7), then when you check for 4 in your search function, it returns true and you answer is 5 (this is not correct) it should be 4.
2. don't use vector to store the generated randomNumber.. searching through it is a pain (linear search), since the order of number is not sorted. my suggestion is use map<int,int> first is the randomNumber, second is the position.
so from my example above 2 4 5 6 7 4, you will have
(2,0) (4,1) (5,2) (6,3) (7,4) and when u get a 4, you find 4 with a position of 1, at this point you already have 5 numbers in your map. so 5 - 1 = 4 (which is the correct answer)
Suppose..
Z= 7
I= 5
M=12
L= 4
Current Random number | Next random number (ZxL+I)%M
4 | 9
9 | 8
8 | 1
1 | 0
0 | 5
5 | 4
Now you got 4 again. So, the length is 6. And thats the output.
Does anyone have more test data beyond what's in the problem description? I keep getting WA for this simple problem and I can't figure out why. (I have also read all other threads related to this problem already) Thanks.
Basically for each pseudo-random number, I keep track of when it was first seen with firstAppearance. Then each time I generate a new term I check this array to see if it was encountered before and if so I can break out of the while loop. Thanks for taking a look at it.
while(true) {
last = ((multiplier%modulo) * (last%modulo) + increment%modulo)%modulo;
/* last = (multiplier * last + increment)%modulo; you can get overflow error*/
if(firstAppearance[last] >= 0) {
break;
}
firstAppearance[last] = 1;
counter++;
}
I think you haven't understood the problem clearly. You should 1st generate a random number and then start calculating. If you get a number which was found previously you should break the loop and print the counter.