سؤال

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

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.

نصائح أخرى

1, generate numbers 1-50

2, multiply all the numbers by 2

all numbers multiplied by 2 are even

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top