Can I set a range of numbers when using arc4random()? For example 50-100 only.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As pointed out in other posts below, it is better to use
arc4random_uniform. (When this answer was originally written,arc4random_uniformwas not available). Besides avoiding the modulo bias ofarc4random() % x, it also avoids a seeding problem witharc4randomwhen used recursively in short timeframes.will generate 0, 1, 2 or 3. Thus you could use:
and merely add 50 to the result to get a range between 50 & 100 (inclusive).