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.
The function arc4random() returns a number from 0 to (2^32) -1
I'm installing a new version of Xcode right now so I can't test it, but you should be able to generate a random number from zero to 1 with code like this:
Code:
double random = arc4random() / ((double) 2<<32 - 1);
thanks for your response, i get "Invalid operands to binary expression ('double' to 'int')"
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.
double random = arc4random() / ((double) (((long long)2<<31) -1));
The "(((long long)2<<31) -1)" bit is just a way to get a constant thats the size of the maximum random number arc4random() will generate. There may be a constant, but I couldn't find it. arc4random generates a larger range of numbers than rand(). It also seeds itself, and generates much better random numbers.
You'll find that unless you seed rand(), it generates exactly the same sequence of "random" numbers every time you run your program.
You can't use "arc4random() % 1" because "%" is the remainder operator. Dividing by 1 doesn't do anything. You need to divide your random number by the largest possible value it can give you.
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.
sorry for being a noob, how do i seed? i just noticed now that the numbers are repeating
as i said i don't need to it be insanely random i'm just playing around so rand is enough for me, and also i think it's good that i can replay the same values if needed for debug. so how do i seed? :P
sorry for being a noob, how do i seed? i just noticed now that the numbers are repeating
as i said i don't need to it be insanely random i'm just playing around so rand is enough for me, and also i think it's good that i can replay the same values if needed for debug. so how do i seed? :P
I haven't used rand() in so long that I can't remember the call to seed it, and the docs are really bad. Baja?
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.
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.
The only thing about arc4random is that I've read somewhere that it was slower than random or rand, but I haven't tested it nor was I in a situation where I needed to call a random number generator 1000 times per second or more, so I've always used arc4random.
The only thing about arc4random is that I've read somewhere that it was slower than random or rand, but I haven't tested it nor was I in a situation where I needed to call a random number generator 1000 times per second or more, so I've always used arc4random.
The only BAD thing I've heard about arc4random is that it's a little slow. It generates MUCH, MUCH better random numbers than either rand() or random().
I just did a little test, and on my iPhone 3GS, which is slow by today's standards, here's what I got:
arc4random()
0.527984
rand()
0.091877
random()
0.077830
So rand() is a little over 5 times faster than arc4rand; Interestingly, random() is the fastest of all.
Clearly, if you care about speed, use random(). It is a better random number generator than rand (which stinks and shouldn't be used.) and also faster.
arc4random generates the best random numbers, and is self-seeding, but it is significantly slower than the others.
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.
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.