Generate a random float between 0 and 1

2019-01-04 22:56发布

I'm trying to generate a random number that's between 0 and 1. I keep reading about arc4random(), but there isn't any information about getting a float from it. How do I do this?

13条回答
Luminary・发光体
2楼-- · 2019-01-04 23:49
(float)rand() / RAND_MAX

The previous post stating "rand()" alone 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 in the
range of 0 to RAND_MAX (as defined by the header file "stdlib.h").

查看更多
登录 后发表回答