c - Xcode 上的 arc4random C 版本

标签 c xcode random srand arc4random

我想使用 2 个变量中的随机数进行乘法测验。 我尝试使用这个:

 int answ

 int rand1= (arc4random()%10)+1;

 int rand2= (arc4random()%20)+1;

 printf("\n¿What is %d * %d?: ", rand1, rand2);

 scanf("%d", &answ);

 if (answ==rand1*rand2)
     printf("\nCorrect answer!");
 else
     printf("\Incorrect answer, the result was %d", rand1*rand2);

我的问题是,当提出下一个问题时,两个随机数都没有改变。 我还尝试添加 srand(time(NULL)) 和相同的..(我已经添加了库 stdlib.h)

缺少什么?

最佳答案

arc4random() 不需要播种,并且将返回与任何先前返回的数字无关的随机数。

您的程序存在问题,导致您没有对 arc4random() 进行新的调用,或者没有使用新值。您如何要求重新运行?

srand(time(NULL))arc4random() 无关。

请注意,您可以使用 arc4random_uniform(10) 代替 arc4random()%10)

关于c - Xcode 上的 arc4random C 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728143/

相关文章:

c - 如何进行百万级词典搜索**(非英语)**

ios - UICollectionView 没有名为 dequeueResuableCellWithReuseIdentifier 的成员

Xcode 委托(delegate)链接到对象

ios - 从 APP Store 上的应用程序中检索代码

ios - 在 Objective C (iOS) 中生成一个介于 0.0001 和 0.002 之间的随机数?

algorithm - 取决于相关种子的可预测随机序列

C - 不使用 strcmp() 按字母顺序对字符串进行排序

c - parent 和多个 child 的 PID。 Fork & waitpid - 函数

c - 错误: initializer element is not constant (how can I implement this?)

java.util.UUID.randomUUID().toString() 长度