generating random odd and even number is sequence

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...
You have a mistake: 1%2 == 1, not 0. That is the reason for that true-evaluation.
If you don't belive me, build this code:
1
2
3
4
5
6
7
#include<iostream>

int main()
{
    std::cout<<1%2<<" ";
    return (0);
}
You can make this by inverting the if-operation with the else-operation, I think.
i know thats what i said
1%2 == 0? Well no it isn't
it is false but it takes it as true
That's right. I've understand wrong. Try that 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()) % 30;				
		else											
			A[i][j] = 37 + (2*rand()+1) % 30;			
	}
}
Any results?
i dont understand what have you changed??
I've replaced the A[i][j] = 37 + (2*rand()) % 30; with A[i][j] = 37 + (2*rand()+1) % 30;. Here are 3 ways of doing that. One of them MUST work. If one of those ways doesn't work, you should change the structure of the program.

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;			
	}
}


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;			
	}
}


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;			
	}
}
Topic archived. No new replies allowed.