Generate a random float between 0 and 1

I am trying to create a random number that is between 0 and 1. I keep reading about arc4random() , but there is no information about getting a float from it. How to do it?

+82
c floating-point ios random arc4random
Mar 02 '11 at 19:32
source share
13 answers

The random value in [0, 1 [ (including 0, excluding 1):

 double val = ((double)arc4random() / UINT32_MAX); 

More details here .

The actual range is [0, 0.9999999996767169356] , since the upper bound is (double) 0xFFFFFFFF / 0x100000000.

+134
Mar 02 '11 at 19:35
source share
 // Seed (only once) srand48(time(0)); double x = drand48(); // Swift version // Seed (only once) srand48(Int(Date().timeIntervalSince1970)) let x = drand48() 

The drand48 () and erand48 () functions return non-negative double-precision floating-point values ​​uniformly distributed over the interval [0.0, 1.0].

+105
Jun 05 '13 at 10:37
source share

For Swift 4.2+ see: stack overflow.site/questions/71372 / ...




The following are recommendations for proper uniformity and optimal accuracy for ObjC and Swift 4.1.

Accuracy 32 bits (optimal for Float )

Uniform random value in [0, 1] (including 0.0 and 1.0) with an accuracy of 32 bits:

Obj-C :

 float val = (float)arc4random() / UINT32_MAX; 

Swift :

 let val = Float(arc4random()) / Float(UInt32.max) 

Optimal for:

  • Float (or Float32 ), which has significance and accuracy of 24 bits for its mantissa

48 bit accuracy (not recommended)

With drand48 it is easy to achieve an accuracy of 48 bits ( which uses arc4random_buf under the hood ). But note that drand48 has flaws due to the need for seed, and also because it is not optimal for randomizing all 52 bits of a double mantissa.

Uniform random value in [0, 1] , accuracy 48 bits:

Swift :

 // seed (only needed once) srand48(Int(Date.timeIntervalSinceReferenceDate)) // random Double value let val = drand48() 

Accuracy 64 bits (optimal for Double and Float80 )

Uniform random value in [0, 1] (including 0.0 and 1.0) with an accuracy of 64 bits:

Swift using two calls to arc4random:

 let arc4random64 = UInt64(arc4random()) << 32 &+ UInt64(arc4random()) let val = Float80(arc4random64) / Float80(UInt64.max) 

Swift using one call to arc4random_buf:

 var arc4random64: UInt64 = 0 arc4random_buf(&arc4random64, MemoryLayout.size(ofValue: arc4random64)) let val = Float80(arc4random64) / Float80(UInt64.max) 

Optimal for:

  • Double (or Float64 ), which has significance and accuracy of 52 bits for its mantissa
  • Float80 which matters and accuracy of 64 bits for its mantissa



Notes

Comparison with other methods

Answers where the range excludes one of the boundaries (0 or 1) are likely to suffer from uniformity bias and should be avoided.

  • using arc4random() , best accuracy 1 / 0xFFFFFFFF (UINT32_MAX)
  • using arc4random_uniform() , the best accuracy is 1 / 0xFFFFFFFE (UINT32_MAX-1)
  • using rand() ( secretly using arc4random ), the best precision is 1 / 0x7FFFFFFF (RAND_MAX)
  • using random() ( secretly using arc4random ), best accuracy 1 / 0x7FFFFFFF (RAND_MAX)

It is mathematically impossible to achieve better accuracy than 32- arc4random with a single call to arc4random , arc4random_uniform , rand or random . Thus, our aforementioned 32-bit and 64-bit solutions should be the best we can achieve.

+14
Jan 13 '16 at 11:36
source share

This function also works with negative floating point ranges:

 float randomFloat(float Min, float Max){ return ((arc4random()%RAND_MAX)/(RAND_MAX*1.0))*(Max-Min)+Min; } 
+10
May 03 '12 at 18:19
source share

Swift 4.2 +

Swift 4.2 adds native support for a random value in the range:

 let x = Float.random(in: 0.0...1.0) let y = Double.random(in: 0.0...1.0) let z = Float80.random(in: 0.0...1.0) 

Doc:

+4
Jun 07 '18 at 4:44
source share
 (float)rand() / RAND_MAX 

The previous post indicating "rand ()" was incorrect. This is the correct way to use rand ().

This will create a number between 0 → 1

BSD docs:

The rand () function computes a sequence of pseudo-random integers ranging from 0 to RAND_MAX (as defined in the header file "stdlib.h").

+1
Jan 31 '13 at 7:33
source share

This is an extension for the random number of Float Swift 3.1.

 // MARK: Float Extension public extension Float { /// Returns a random floating point number between 0.0 and 1.0, inclusive. public static var random: Float { return Float(arc4random()) / Float(UInt32.max)) } /// Random float between 0 and n-1. /// /// - Parameter n: Interval max /// - Returns: Returns a random float point number between 0 and n max public static func random(min: Float, max: Float) -> Float { return Float.random * (max - min) + min } } 
+1
Sep 06 '17 at 1:51 on
source share

Swift 4.2

Swift 4.2 included in the standard library its own and fairly full-featured random number API. ( Offer Swift Evolution SE-0202 )

 let intBetween0to9 = Int.random(in: 0...9) let doubleBetween0to1 = Double.random(in: 0...1) 

All types of numbers have a static function random (in :), which takes a range and returns a random number in a given range.

+1
Jun 10 '18 at 18:29
source share

Use this to avoid problems with the top border of arc4random ()

 u_int32_t upper_bound = 1000000; float r = arc4random_uniform(upper_bound)*1.0/upper_bound; 

Please note that it is applicable for MAC_10_7, IPHONE_4_3 and above.

0
Feb 28 '14 at 17:51
source share

What about this operation ((CGFloat)(rand()%100)/100) ?

-one
Feb 12
source share

arc4random has a range of up to 0x100000000 (4294967296)

This is another good opportunity to generate random numbers from 0 to 1:

 srand48(time(0)); // pseudo-random number initializer. double r = drand48(); 
-one
Sep 27 '17 at 5:31 on
source share
 float x = arc4random() % 11 * 0.1; 

This creates a random float value of 0 and 1. Read more here

-four
Jan 26 '12 at 17:18
source share
 rand() 

by default creates a random number (float) between 0 and 1.

-four
Oct 31 '12 at 18:35
source share



All Articles