1

With (float)arc4random() how can I generate a float random number included in [0, 1[ i.e. in the interval 0-1, with 0 included and 1 excluded?

My code is

  do { 
          c = ((float)arc4random() / 0x100000000);
     }
  while (c == 1.0);

Is there anything better?

boscarol
  • 1,901
  • 2
  • 15
  • 29

4 Answers4

3

It depends how many possible numbers you want in between the two?

But you can use...

float numberOfPossibilities = ...;
float random = (float)arc4random_uniform(numberOfPossibilities) / numberOfPossibilities;

To exclude 1 you could do...

float random = (float)arc4random_uniform(numberOfPossibilities - 1) / numberOfPossibilities;
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • How many possibilities? All possible float numbers between 0 and 1, but 1.000000 excluded. – boscarol Oct 02 '13 at 13:22
  • There are infinitely many, good luck getting that to work. You have to compromise down to a sensible level. What are you doing? Drawing pixels on the screen? Then 2000 would be a good level. Think of what you are doing with the number and then compromise. – Fogmeister Oct 02 '13 at 13:22
  • Perhaps do { c = ((float)arc4random() / 0x100000000);} while (c == 1.0); – boscarol Oct 02 '13 at 13:29
  • You shouldn't do `arc4random()/something` it doesn't work well. What are you actually trying to do? i.e. what will you be using the random number for? – Fogmeister Oct 02 '13 at 13:31
  • I learned that float c1 = ((float)arc4random() / ARC4RANDOM_MAX) is the correct way to get a random number between 0 and 1 (with #define ARC4RANDOM_MAX 0x100000000) – boscarol Oct 02 '13 at 13:38
  • Like I said in my previous comment. Doing `arc4random()/something` is not a good idea. – Fogmeister Oct 03 '13 at 06:46
  • Then first answer here is wrong: [link](http://stackoverflow.com/questions/5172421/generate-a-random-float-between-0-and-1) – boscarol Oct 03 '13 at 10:43
2
// Get a value greater than the greatest possible random choice
double one_over_max = UINT32_MAX + 1L;
// Use that as the denominator; this ratio will always be less than 1
double half_open_result = arc4random() / one_over_max;

The resolution -- the number of possible resulting values -- is thus the same as the resolution of the original random function. The gap between the largest result and the top of the interval is the difference between your chosen denominator and the original number of results, over the denominator. In this case, that's 1/4294967296; pretty small.

jscs
  • 63,694
  • 13
  • 151
  • 195
0

This is extension for 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()) / 0xFFFFFFFF
    }

    /// 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
    }
}
YanSte
  • 10,661
  • 3
  • 57
  • 53
-2

you can use like:

Float num = (arc4random() % ([[filteredArrayofPerticularword count] FloatValue]));

In that filteredArrayofPerticularword array u can store your number.

Bhanu
  • 1,249
  • 10
  • 17
  • 1
    arc4random doesn't play well with using modulo mathematics. I have been down voted in the past for suggesting it :( also, this will generate a number between 0 and the count of the array. Not between 0 and 1. – Fogmeister Oct 02 '13 at 13:19
  • 1
    Use [`arc4random_uniform(u_int32_t)`](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/arc4random.3.html) instead. – i_am_jorf Feb 15 '15 at 20:42