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() % n0n-1
6
arc4random() % n
0
n-1
#include <stdlib.h>
int r = arc4random() % 6;
結果は 0 ~ 5 です。1 ~ 6 は +1 で調整します。