Hello guys,
I'm in need of assistance. I appreciate any help!
I want to generate random even and odd numbers is sequence pretty much like this: 1st number is odd, 2nd is even, 3rd is odd, 4th is even, and so on (note: all random numbers has to be between 37 and 67)
No i don't want you to make this function for me, but i just want you to explain something for me. This is a function to fill a two dimensional array called A[rowsA][columnsA].
First here is my function:
1 2 3 4 5 6 7 8
|
for(int i=0; i<rowsA; i++){
for(int j=0; j<columnsA; j++){
if(j%2 == 0)
A[i][j] = 37 + (2*rand()) % 30;
else
A[i][j] = 37 + (2*rand()+1) % 30;
}
}
|
It is generating the random numbers correctly with no problems, and if i want to generate the numbers in the opposite way (ex: 1st is even, 2nd is odd, 3rd is even, 4th is odd) i simply do it this way:
1 2 3 4 5 6 7 8
|
for(int i=0; i<rowsA; i++){
for(int j=0; j<columnsA; j++){
if(j%2 == 0)
A[i][j] = 37 + (2*rand()+1) % 30;
else
A[i][j] = 37 + (2*rand()) % 30;
}
}
|
I wrote the code 1 week ago but yesterday i came to review it, i didn't understand the
if(j%2 == 0)
In the first case, when i=0 and j=0, the condition will be checked, 0%2 == 0? No so it will generate a odd number. So far no problems. Second comes when i=0 and j=1, the condition will be checked, 1%2 == 0? Well no it isn't, but it takes it as true and it generate a even number. The other way around :S
Can you care to explain please? I know it could be something so easy i will hit myself after i figure it out, but please gimme a break i'm new to all this.
Regards, and thanks in advance...