【发布时间】:2021-02-08 15:57:27
【问题描述】:
我是 C 和 GBDK 的新手,我想编写一个在 0 和 1 之间决定的随机数生成器。 就像一个“黑客”模拟器。
我尝试了很多来自 Internet 的示例。但没有一个有效。
我上次尝试的输出截图:https://i.ibb.co/f8G39vX/bgberrors.png
上次尝试代码:
#include <gb/gb.h>
#include <stdio.h>
#include <rand.h>
void init();
void main()
{
init();
while(1)
{
UINT8 r = ((UINT8)rand()) % (UINT8)4;
printf(r);
}
}
void init()
{
DISPLAY_ON;
}
我怎样才能完成它?
【问题讨论】:
-
您需要
float或double才能从0 到1,而在C 中printf()不能这样工作。例如printf("%f\n", 1.0 * rand() / RAND_MAX); -
别忘了
seed你的RNG。 -
@WeatherVane 'main.c:12: error 20: Undefined identifier 'RAND_MAX'' 这在构建时出现。
-
请
#include <stdlib.h>(并删除rand.h,但也许GBDK有自己的最大值)。如果您对某些东西不熟悉,则应该前往手册页。 -
@WeatherVane 'main.c:12:警告 112:函数 'rand' 隐式声明。 main.c:12: error 20: Undefined identifier 'RAND_MAX'' 现在有一个警告。我很困惑:s