to pick a number between 25 and 455. I am now trying to now pick a number between 65 and 455. I have tried replacing the 25 at the start of the code to 65 but this sometimes gives me numbers greater than 455.
to pick a number between 25 and 455. I am now trying to now pick a number between 65 and 455. I have tried replacing the 25 at the start of the code to 65 but this sometimes gives me numbers greater than 455.
Thanks in advance for any help!!
The code you posted will create random numbers between 25 and 435
In general, the code
Code:
int smallest = 25;
int largest = 500;
int random = smallest + arc4random() %(largest+1-smallest);
should give you a number in the range smallest ... largest
You could create a simple method to give you random numbers in a range:
Code:
- (unsigned long) randomFromSmallest: (unsigned long) smallest toLargest (unsigned long) largest
{
int smallest = 25;
int largest = 500;
int random = smallest + arc4random() % (largest+1-smallest);
return random;
}
And use it anyplace you need random numbers in a range.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Great answer but I use a smaller method and it works just as fine. Can anyone tell me the diffrance?
In my game i generate random number between 1 and 10 and I use the code
teleportIndex = arc4random() % 10+1;
SOO.... wouldnt we just have to do
YOURVariable = arc4random() % 455+25;
No.
This arc4random() % 455 will give you a number between 0 and 454. Adding 25 to it will give you a number between 25 and 479. But the OP needs a number between 25 and 455. That why you use what Duncan said.
If you think about it your are doing the same
Simply put where smallest = 1 and largest = 10 and you'll have
int random = 1 + arc4random() % (10+1-1);
which is
int random = 1 + arc4random() % 10;
__________________ SQLed - Your Database Manager on the go
No.
This arc4random() % 455 will give you a number between 0 and 454. Adding 25 to it will give you a number between 25 and 479. But the OP needs a number between 25 and 455. That why you use what Duncan said.
If you think about it your are doing the same
Simply put where smallest = 1 and largest = 10 and you'll have
int random = 1 + arc4random() % (10+1-1);
which is
int random = 1 + arc4random() % 10;
Yea but I can visualy see that the method I wrote actually works, it never genrates a number greater than 10. I have it right infront of me on a label and I have been testing my game like this for months, if it generated the number 11, i would know because the game would not produce a stage and that never happened.
I appriciate your reply but I dont get how your answer is valid since my way works 100% and I can see it.
I will look into duncans method for future coding but I just find this curious
that generates a number from 0 to 9. so by adding 1 like
variable = arc4random() % 10 +1;
I move it all up so it now generates a number between 1 and 10. so YES if we add 25, it will just shift everything up so Duncans method is the only valid way to do this, I just got lucky with mine since my values start at 1.
that generates a number from 0 to 9. so by adding 1 like
variable = arc4random() % 10 +1;
I move it all up so it now generates a number between 1 and 10. so YES if we add 25, it will just shift everything up so Duncans method is the only valid way to do this, I just got lucky with mine since my values start at 1.
Exactly. This % is the symbol for modulo. That's why arc4random() % 10 will give you numbers between 0 and 9.
If you read carefully through my previous post, you'll see that I'm saying that what you do works because you are using Duncans method.
__________________ SQLed - Your Database Manager on the go