1

我是 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;
}

我怎样才能实现它?

4

1 回答 1

4
#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 有它自己的库(包括标准库)。它可能是 20 年前 SDCC 的 lib 的一个分支。当前的 SDCC 有rand()stdlib.h但 GBDK-2020 没有。Max 是 0xFF,我不知道它的定义。

浮动应该尽可能的避免,它完全是在软件中完成的,没有硬件支持。编译器并不真正支持 Double 并回退到 float。

没有手册页,此处提供文档:https : //gbdk-2020.github.io/gbdk-2020/docs/api/rand_8h.html 或阅读 gbdk_manual.pdf 与 gbdk-2020 一起使用

于 2021-04-21T20:51:46.820 回答