开发者

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

开发者 https://www.devze.com 2022-12-15 02:49 出处:网络
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.

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 Num开发者_Python百科ber 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?


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


i think that the default of the size of the number returned from arc4random() is 6 digits. try putting 3,4,5 as arguments like arc4random(4);and see the results.

0

精彩评论

暂无评论...
验证码 换一张
取 消