Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在制作一个 iPhone 骰子应用程序,我想创建一个按钮,这样如果我单击它,它将在 UITextField 上生成一个介于 1 和 6 之间的随机数。
任何人都可以帮助我并给我一个数学公式来生成这样的数字吗?
谢谢
如果您使用的是 Objective-C,请尝试:
#include <stdlib.h> int random_number = (arc4random() % 6) + 1;
6是必需的,因为arc4random() % n结果从0到n-1。
6
arc4random() % n
0
n-1
#include <stdlib.h>
int r = arc4random() % 6;
结果介于 0 到 5 之间。用 +1 调整 1 到 6。