【问题标题】:arc4random on Xcode for CXcode for C 上的 arc4random
【发布时间】:2015-01-01 03:47:43
【问题描述】:

我想使用来自 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)

缺少什么?

【问题讨论】:

    标签: c xcode random srand arc4random


    【解决方案1】:

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

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

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

    请注意,您可以使用arc4random_uniform(10),而不是arc4random()%10)

    【讨论】:

    • 我将 arc4random()%10 换成了 arc4random_uniform(10),但还是一样。但我解决了,谢谢!正如你所说,我不得不要求重新运行!
    • 是的,这不是问题。 arc4random 可以正常工作,这就是您使用它的方式,其余的代码在哪里问多个问题?提供的代码甚至无法编译,它有两个错误。
    猜你喜欢
    • 2023-03-14
    • 2017-04-05
    • 2013-07-12
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多