1

I'm new to C and to GBDK and I want to code a random number generator that decides between 0 and 1. Like a 'hacker' simulator.

I have tried a lot of examples from the Internet. But none worked.

Screenshot from the output of the last attempt I made: https://i.ibb.co/f8G39vX/bgberrors.png

Last attempt code:

#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;
}

How can I accomplish it?

  • You need `float` or a `double` to be able to range from 0 to 1, and in C `printf()` does not work like that. For example `printf("%f\n", 1.0 * rand() / RAND_MAX);` – Weather Vane Feb 08 '21 at 16:00
  • 1
    Don't forget to `seed` your RNG. – 0x5453 Feb 08 '21 at 16:00
  • @WeatherVane 'main.c:12: error 20: Undefined identifier 'RAND_MAX'' This appeared when building. – Aarón García Egea Feb 08 '21 at 16:10
  • Please `#include ` (and remove `rand.h` but perhaps GBDK has its own max value). if you are new to something you should head for the man pages. – Weather Vane Feb 08 '21 at 16:15
  • @WeatherVane 'main.c:12: warning 112: function 'rand' implicit declaration. main.c:12: error 20: Undefined identifier 'RAND_MAX'' Now with a warning. I'm getting confused :s – Aarón García Egea Feb 08 '21 at 16:23
  • I am sorry but if you are new to C and none of the examples you find work, then page 1 of the tutorial is the place to start. – Weather Vane Feb 08 '21 at 16:25
  • Do you want to randomly select one of the strings "0" and "1" for output? Also, it looks like GBDK has its own non-standard C library, `rand` is in rand.h and returns an INT8. You likely want to use `putchar` to output either `'0'` or `'1'` based on the result. – Hasturkun Feb 08 '21 at 18:57
  • 1
    Untested, `putchar((rand() % 2) ? '1' : '0')`. or longer form, `char c; if ((rand() % 2) == 0) {c = '0';} else {c = '1';} putchar(c);` Not making this an answer yet as I can't test. – Hasturkun Feb 09 '21 at 01:25
  • OMG @Hasturkun it worked! The longer form worked first try! I only needed to include stdio.h – Aarón García Egea Feb 09 '21 at 14:16

1 Answers1

5
#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;
}

Tested with GBDK-2020 4.0.3

Also check the "rand" example in GBDK-2020.

Regarding the comments:

Yes, GBDK has it's own lib (including stdlib). It's probably a fork of SDCC's lib 20 years ago. Current SDCC has rand() in stdlib.h, but GBDK-2020 doesn't. Max is 0xFF, I don't know of a define for that.

Float should be avoided as much as possible, it's completely done in software, there is no hardware support for this. Double isn't really supported by the compiler and falls back to float.

There are no man pages, documentation is available here: https://gbdk-2020.github.io/gbdk-2020/docs/api/rand_8h.html or read the gbdk_manual.pdf comming with gbdk-2020

basxto
  • 63
  • 1
  • 5