views:

448

answers:

2

Hello, 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?

A: 

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.

Bass
It doesn´t work. He says: "Too many arguments to function ´arc4random´
Flocked
+2  A: 

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

Mike
Hm, It doesn´t work, too. When I set (arc4random() % 100000) + 1; I always get a number with 5 digits.When I see it right, you just add 1 to the "random" number.
Flocked
What platform are you using? According to http://iphonedevelopment.blogspot.com/2008/10/random-thoughts-rand-vs-arc4random.html, this will have a range of 0 to 0x100000000. Also, the numbers *ARE* random, so you may just be hitting the numbers with 5 digits by pure chance.
Mike
I use it with with the iPhone OS 3.2.1 - It is not pure chance, because I always get 5 digits with 100000 and 4 digits with 10000, 3 with 1000 and so on...The 5 digit-numbers are random but not the lenght.
Flocked
`(arc4random() % 100000) + 1` should almost ALWAYS generate numbers with 5 digits or less. That statement generates numbers from 1 to 100000. The only number which it could generate which has 6 digits would be 100000, which has a probability of .00001 of occurring(assuming uniform distribution).
Mike