163k views
2 votes
How do I write: "get a random number between -10 and 11 but it cannot be 0"? (java)

User Andrene
by
5.6k points

1 Answer

0 votes

Answer:

generate a random number in a known range and map the result to the range you want

Step-by-step explanation:

Use any of the usual methods to get a random number in the range 0-20, then subtract 9. Use an IF statement to test if the result is 0. If it is, replace the value with -10.

User Stephen Quan
by
4.8k points