Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Write code that generates a random odd integer (not divisible by 2) between 50 and 99 inclusive. Fill in the values of the sub-expressions labeled A, B, and C below.

Random rand = new Random(); int n = rand.nextInt(A) * B + C;



The answer is A = 25; B = 2; C =51

What I have tried:

I dont get the answer. rand.nextInt(25) meansa range of  0-24 right.  Then, you multiply by 2 so the range becomes 0-48. Last you add 51 so the final range becomes  51 - 99, but the problem says the range is 50-99. Can someone explain to me how to get range of 50-99 with answers of A = 25; B = 2; C =51
Posted
Updated 9-Oct-17 21:33pm
Comments
Mohibur Rashid 10-Oct-17 1:51am    
Your question is not clear enough to answer. It also looks like A, B, C are constant to fulfill the requirement of bigger than 50, odd only and less than 100.

May be discuss with your teacher, first, about your assignment

You are required to generate odd numbers only: so the value you add on - C - must be odd, and the original value - rand.nextInt(A) * B must be even, which means that B must be even.
So your tutor has selected B = 2, C = 51 which fulfils the "must be odd" requirement, and offsets the number so that it is 51 or greater under all circumstances. Since 51 is the first odd value greater than 50 it fits the low end of the range as well.
A controls the original range of values: 25 will indeed give you numbers from 0 to 24, so the complete expression will give values like this:
A = 0 :  0 * 2 + 51 ==  0 + 51 == 51
A = 1 :  1 * 2 + 51 ==  2 + 51 == 53
A = 2 :  2 * 2 + 51 ==  4 + 51 == 55
...
A = 22: 22 * 2 + 51 == 44 + 51 == 95
A = 23: 23 * 2 + 51 == 46 + 51 == 97
A = 24: 24 * 2 + 51 == 48 + 51 == 99
Which fits all your criteria.
 
Share this answer
 
Comments
CPallini 10-Oct-17 3:31am    
5.
Quote:
51 - 99, but the problem says the range is 50-99.
Indeed taht's the range. But, unfortunately 50 is even, hence 51 is the first valid entry of the requested range.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900