Random[i]
Would you tell me how to implement random[i]=(a*random[i-1]+b)%c ?
The following code should be a crazy implementation, since I really don't understand how to do this...
int Rand(){
static int i=0;
int r[5];
r[0]=1;
return (r[++i]=(a*r[i]+b)%c);
}
Thanks
[295 byte] By [
Beck2] at [2007-11-19 7:40:17]

# 1 Re: Random[i]
First of all, the variables a, b, c must have values. random[0] as well must have an initial value. then with n being the index of the last element you want to compute, you can proceed as
for (i=1;i<=n;i++) random[i] = (a * random[i-1] + b) % c;
May be you do not need all that numbers, but only the last one. In this case, simply use a variable, say, random_number with some initial value.
for (i=1;i<=n;i++) random_number = (a * random_number + b) % c;
T.G. at 2007-11-9 0:42:46 >

# 2 Re: Random[i]
void foo(int *random, int size, int a, int b, int c)
{
random[0] = 1;
for(int i=1; i<size; i++)
random[i] = (a*random[i-1]+b) % c;
}
int main()
{
int a = 1;
int b = 2;
int c = 3;
int array[100];
memset(array, 0, sizeof(array));
foo(array, 100, a,b,c);
for(int i=0; i<100; i++)
cout << array[i] << endl;
}
cilu at 2007-11-9 0:43:49 >

# 3 Re: Random[i]
Would you tell me how to implement random[i]=(a*random[i-1]+b)%c ?
The following code should be a crazy implementation, since I really don't understand how to do this...
int Rand(){
static int i=0;
int r[5];
r[0]=1;
return (r[++i]=(a*r[i]+b)%c);
}
Thanksr array is not static, so on second run this function would return really random number :rolleyes: from somewhere in the stack. i would be equal to 1 by then, and r[1] would be uninitialized.