Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


+15 votes

I am confused about how to generate a random number to either add or subtract 1 from the initial number.

asked in CSC211_Winter2018 by (8 points)

2 Answers

+10 votes

Think about generating two numbers, perhaps 0 and 1. Then associate add 1 to one of the two options and subtract 1 to the other using an if....else.

answered by (7.9k points)
+1 vote

You can also take the random number that's 0 or 1, and multiply it by 2, and then subtract one from that, to get either -1 or 1.

answered by (12.2k points)