Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-12-2011, 10:02 AM   #1 (permalink)
Registered Member
 
vunterslaush's Avatar
 
Join Date: Aug 2011
Posts: 16
vunterslaush is on a distinguished road
Default how do i get a random number between 0 and 1?

such as 0.423, 0.0001, 0.123214, etc
thanks. don't has to be insanely random or something, for a simple game.
vunterslaush is offline   Reply With Quote
Old 08-12-2011, 10:20 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vunterslaush View Post
such as 0.423, 0.0001, 0.123214, etc
thanks. don't has to be insanely random or something, for a simple game.
The code below should return a double precision float from 0.0 to 1.0:

Code:
double random = arc4random() / ((double) (((long long)2<<31) -1));
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 08-12-2011 at 10:37 AM.
Duncan C is offline   Reply With Quote
Old 08-12-2011, 10:30 AM   #3 (permalink)
Registered Member
 
vunterslaush's Avatar
 
Join Date: Aug 2011
Posts: 16
vunterslaush is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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')"

this is the code that i wrote:
Code:
-(double) rand{
    return arc4random() / ((double) 2<<32 - 1);
}
vunterslaush is offline   Reply With Quote
Old 08-12-2011, 05:03 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vunterslaush View Post
thanks for your response, i get "Invalid operands to binary expression ('double' to 'int')"

this is the code that i wrote:
Code:
-(double) rand{
    return arc4random() / ((double) 2<<32 - 1);
}
I tested, edited and fixed my post. Try the updated version.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-12-2011, 05:19 PM   #5 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

Sorry, Duncan, what is the
Code:
2<<32 - 1
And what is the benefit over
Code:
arc4random() % 1
sneaky is offline   Reply With Quote
Old 08-13-2011, 04:51 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Roermond, Netherlands
Posts: 5
cujo30227 is on a distinguished road
Default

Here's a simple random number generator

Code:
float random = rand()/(float)RAND_MAX;
gives you a number between 0 and 1.
cujo30227 is offline   Reply With Quote
Old 08-13-2011, 05:42 AM   #7 (permalink)
Registered Member
 
vunterslaush's Avatar
 
Join Date: Aug 2011
Posts: 16
vunterslaush is on a distinguished road
Default

Quote:
Originally Posted by cujo30227 View Post
Here's a simple random number generator

Code:
float random = rand()/(float)RAND_MAX;
gives you a number between 0 and 1.
thanks a lot that worked perfectly!
vunterslaush is offline   Reply With Quote
Old 08-13-2011, 06:23 AM   #8 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Keep in mind that rand() needs to be seeded.
baja_yu is offline   Reply With Quote
Old 08-13-2011, 06:41 AM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sneaky View Post
Sorry, Duncan, what is the
Code:
2<<32 - 1
And what is the benefit over
Code:
arc4random() % 1

The corrected line is


Code:
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-13-2011, 07:58 AM   #10 (permalink)
Registered Member
 
vunterslaush's Avatar
 
Join Date: Aug 2011
Posts: 16
vunterslaush is on a distinguished road
Default

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
vunterslaush is offline   Reply With Quote
Old 08-13-2011, 09:09 AM   #11 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vunterslaush View Post
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?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-13-2011, 09:15 AM   #12 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You can do it by using srand() which takes an unsigned integer as a parameter. People usually pass it the time/date or some other changing number.
baja_yu is offline   Reply With Quote
Old 08-13-2011, 09:25 AM   #13 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Duncan C View Post
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?

Note that the docs recommend that you not use rand. The lower 12 bits or so of the numbers it generates have a repeating (non-random) pattern.

These bits are taken from the docs:

Quote:
rand, rand_r, srand, sranddev -- bad random number generator

Quote:
These interfaces are obsoleted by random(3).
You're much better off using random() or arc4random.

random() also has a seed method, srandom, but produces much better random numbers than rand().

arc4random is the best, and produces very good random numbers.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-13-2011, 09:47 AM   #14 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 08-13-2011, 10:41 AM   #15 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by baja_yu View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-08-2012, 08:56 AM   #16 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 16
sandromandro is on a distinguished road
Default

i want to understand your code but it doesn't work in my project.
is it possible to create random numbers exactly between 0.035 and 0.009?

thank you
sandromandro is offline   Reply With Quote
Old 02-08-2012, 10:34 AM   #17 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sandromandro View Post
i want to understand your code but it doesn't work in my project.
is it possible to create random numbers exactly between 0.035 and 0.009?

thank you
This code will generate a random number from 0 to 1:


Code:
double random = arc4random() / ((double) (((long long)2<<31) -1));
So, you need to divide your result by (largest-smallest), and add the smallest value to it.

Code:
- (double) randomFromMinimum: (double) min toMax: (double) max;
{
  double result;
  double random = arc4random() / ((double) (((long long)2<<31) -1));
  result = random / (max-min) + min;
  return result;
}
That is off the top of my head, without testing, while I'm deep in some nasty video rendering OpenGL code. It will need to be tested and debugged.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
13 members and 383 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, dre, fiftysixty, gmarro, jimmyon122, jonathandeknudt, pungs, raymng, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0