Generating Even Random Numbers

2020-05-06 11:42发布

I need a code to generate only random EVEN numbers 2-100
There is tutorials on the web that generate random numbers but they're odd and even. Please understand i just need even numbers to generate.

2条回答
一夜七次
2楼-- · 2020-05-06 12:12

1, generate numbers 1-50

2, multiply all the numbers by 2

all numbers multiplied by 2 are even

查看更多
放我归山
3楼-- · 2020-05-06 12:17

This will work:

NSInteger evenNumber = (arc4random_uniform(50) + 1) * 2;

arc4random_uniform(50) will give results in the range 0 - 49. Adding 1 gives a value in the range 1 - 50. Multiplying by two gives you even numbers in the range 2 - 100.

查看更多
登录 后发表回答