Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
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 (8 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 (8 points)