Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random number with a random numberlength in Objective-C

always when I try to generate a random number with Int = arc4random % MAXNUM the length of the number is as long as the MAXNUM number length -1. So if I set arc4random % 1000000 the number is between 100000-999999. How can I get different numbers like 78, 476 or 4842? Here a sample:

 int number = arc4random() % 1000000;
 outputLabel.text = [NSString stringWithFormat:@"The Number is %d", number];

Thanks for your help and sorry for my bad English!

Update: Okay I just saw, that I dont get only 5-digit-numbers, but most of the time. Is there a way to get more often lower numbers or numbers with less digits?

like image 654
Flocked Avatar asked Jan 03 '10 01:01

Flocked


1 Answers

If you want to get a random integer in the range x to y, you can do that by int randomNumber = (arc4random() % y) + x;

like image 84
Mike Avatar answered Sep 28 '22 22:09

Mike