Why Use Float(Arc4Random())/0Xffffffff Instead of Drand()

Why use Float(arc4random()) / 0xFFFFFFFF instead of drand()

drand48() is fine for lots of applications, but is insecure (in other words predictable). arc4random() while not perfect was designed with security in mind.

I think Apple pushes people to arc4random() because of that. So, to answer your question: if you are generating random numbers to simulate something, drand48 should be fine, but if you are generating random numbers to protect something, then use arc4random() (or something even more secure like SecRandomCopyBytes()).


From ONLamp's Secure Programming Techniques:

drand48( ), lrand48( ), and mrand48( )

The drand48( ) function is one of many functions that make up the System V random number generator. According to the Solaris documentation, the algorithm uses "the well-known linear congruential algorithm and 48-bit integer arithmetic." The function drand48( ) returns a double-precision number that is greater than or equal to 0.0 and less than 1.0, while the lrand48( ) and mrand48( ) functions return random numbers within a specified integer range. As with random( ), these functions provide excellent random numbers for simulations and games, but should not be used for security-related applications such as picking cryptographic keys or simulating one-time pads; linear congruential algorithms are too easy to break.

Swift randomFloat issue: '4294967295' is not exactly representable as 'Float'

Simplest solution: abandon your code and just call https://developer.apple.com/documentation/coregraphics/cgfloat/2994408-random.

Generate a random float between 0 and 1

Random value in [0, 1[ (including 0, excluding 1):

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

A bit more details here.

Actual range is [0, 0.999999999767169356], as upper bound is (double)0xFFFFFFFF / 0x100000000.

Swift random float between 0 and 1

Try initializing the divisor as a float as well, a la:

CGFloat(Float(arc4random()) / Float(UINT32_MAX))

How to generate a random float number between 0 (included) and 1 (excluded)

// 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.

Swift expression was too complex to be solved in reasonable time

Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions?

static func random(min: CGFloat, max: CGFloat) -> CGFloat {
let rand = CGFloat(arc4random()/0xFFFFFFFF)
return (rand * (max - min) + min)
}

You can also use UINT32_MAX (or the more "Swifty" UInt32.max or .max) in place of 0xFFFFFFFF to improve readability. If I recall, 0xFFFFFFFF is the hex value of the max value of an unsigned 32-bit Integer as defined in the <stdint.h> header.

#define UINT32_MAX 0xffffffff  /* 4294967295U */

What's wrong with this randomize function?

The manual page for arc4random indicates that the returned value can be anywhere in the range valid for u int32 (i.e. 0 to (2**32)-1). This means you'll want to divide by 0xFFFFFFFF, instead of RAND_MAX, which I would imagine is less (it is library dependant however, so you'll have to check exactly what it is).

Your function should thus become:

- (float)randomValueBetween:(float)low andValue:(float)high {
return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}

What's the most optimal way to get a random floatingpoint number between floatA and floatB?

try this

float a = 12.49953f;
float b = 39.11234f;
int startVal = a*10000;
int endVal = b*10000;

srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
int randomValue = startVal+ (random() % (endVal - startVal));

float r = (float)randomValue / 10000.0f;

How to generate a random float number between 0 (included) and 1 (excluded)

// 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.

What is the 0xFFFFFFFF doing in this example?

As you've stated,

arc4random returns an unsigned integer up to (2^32)-1

0xFFFFFFFF is equal to (2^32)-1, which is the largest possible value of arc4random(). So the arithmetic expression (arc4random() / 0xFFFFFFFF) gives you a ratio that is always between 0 and 1 — and as this is an integer division, the result can only be between 0 and 1.



Related Topics



Leave a reply



Submit