我是C语言和GBDK的新手,我想编写一个随机数生成器,它可以在0和1之间做出决定,就像一个“黑客”模拟器。
我已经尝试了互联网上的很多例子。但没有一个管用。
来自我上次尝试的输出的屏幕截图: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;
}我如何才能做到这一点?
发布于 2021-04-22 04:51:47
#include <gb/gb.h>
#include <stdint.h>
#include <stdio.h>
#include <rand.h>
void init();
void main()
{
init();
printf(" \n\n\n\n\n\n\n\n PRESS START!\n");
// abuse user input for seed generation
waitpad(J_START);
uint16_t seed = LY_REG;
seed |= (uint16_t)DIV_REG << 8;
initrand(seed);
while(1)
{
UINT8 r = ((UINT8)rand()) % (UINT8)2;
printf("%d", r);
}
}
void init()
{
DISPLAY_ON;
}使用GBDK-2020 4.0.3进行测试
还可以查看GBDK-2020中的"rand“示例。
关于评论:
是的,GBDK有它自己的库(包括stdlib)。它可能是SDCC 20年前的lib的一个分支。当前的SDCC在stdlib.h中有rand(),但是GBDK-2020没有。Max是0xFF,我不知道它的定义。
Float应该尽量避免,它完全是在软件中完成的,没有硬件支持。编译器并不真正支持Double,而是回退到float。
这里没有手册页,文档可以在这里找到:https://gbdk-2020.github.io/gbdk-2020/docs/api/rand_8h.html或阅读gbdk_manual.pdf -2020
https://stackoverflow.com/questions/66105001
复制相似问题