Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
random_distribution.c

Statistical random distributions with interactive histograms.

Statistical random distributions with interactive histograms.Random distributions generate numbers following specific probability patterns. This example demonstrates three common distributions:

  • Uniform: All values in a range are equally likely (dice rolls, card shuffles)
  • Bernoulli: Binary true/false outcomes with probability p (coin flips, hit chances)
  • Normal (Gaussian): Bell curve around a mean (measurement errors, natural variation)

The example visualizes each distribution as a histogram, showing how random samples converge to the expected statistical shape as sample count increases.

#include <banjo/app.h>
#include <banjo/main.h>
#include <banjo/bitmap.h>
#include <banjo/draw.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/memory.h>
#include <banjo/pixel.h>
#include <banjo/random.h>
#include <banjo/system.h>
#include <banjo/window.h>
#define FB_PIXEL_MODE BJ_PIXEL_MODE_XRGB8888
#define WINDOW_W 800
#define WINDOW_H 600
#define BORDER_W 25
#define BORDER_H 15
#define GRAPH_W (WINDOW_W - BORDER_W * 2)
#define GRAPH_H (WINDOW_H - 100)
#define N_DISTRIBUTIONS 3
typedef struct {
const char* name;
size_t result[GRAPH_W];
uint32_t color;
size_t min_y; /* fixed to 0 for histograms */
size_t max_y; /* per-distribution */
size_t n_steps;
size_t n_steps_base = 524288;
static void init_distributions(void) {
distributions[0].name = "uniform: %ld draws in [0;Xmax[ ; y = how many x";
distributions[0].color = bj_get_pixel_value(FB_PIXEL_MODE, 110, 231, 183);
distributions[0].min_y = 0;
distributions[0].max_y = 0;
distributions[1].name = "bernoulli: %ld draws with a probability p (x) ; y = how many hits";
distributions[1].color = bj_get_pixel_value(FB_PIXEL_MODE, 147, 197, 253);
distributions[1].min_y = 0;
distributions[1].max_y = 0;
distributions[2].name = "normal: %ld draws with Xmax/2 (mean) and 100 (deviation) ; y = how many x";
distributions[2].color = bj_get_pixel_value(FB_PIXEL_MODE, 196, 181, 253);
distributions[2].min_y = 0;
distributions[2].max_y = 0;
}
static void run_distributions() {
for (size_t d = 0; d < N_DISTRIBUTIONS; ++d) {
bj_memzero(distributions[d].result, sizeof(size_t) * GRAPH_W);
distributions[d].min_y = 0;
distributions[d].max_y = 0;
}
distributions[1].n_steps = n_steps_base / 128;
distributions[2].n_steps = n_steps_base / 64;
// Uniform distribution: generates integers uniformly across [min, max].
// bj_uniform_int32_distribution(generator, gen_data, min, max)
// Every value in the range has equal probability. Use this for:
// - Random array indices, positions, rotations
// - Shuffling, dealing cards, rolling dice
for (size_t s = 0; s < distributions[0].n_steps; ++s) {
size_t y = ++distributions[0].result[x];
if (y > distributions[0].max_y) distributions[0].max_y = y;
}
// Bernoulli distribution: generates true/false with probability p.
// bj_bernoulli_distribution(generator, gen_data, p)
// Returns 1 with probability p, 0 with probability (1-p). Use this for:
// - Coin flips, critical hit chances, spawn decisions
// - Any yes/no outcome with a specific probability
// Here we vary p across x to show the linear relationship.
for (size_t px = 0; px < GRAPH_W; ++px) {
bj_real p = (bj_real)px / GRAPH_W;
for (size_t s = 0; s < distributions[1].n_steps; ++s) {
size_t y = ++distributions[1].result[px];
if (y > distributions[1].max_y) distributions[1].max_y = y;
}
}
}
// Normal (Gaussian) distribution: generates values in a bell curve.
// bj_normal_real_distribution(generator, gen_data, mean, std_deviation)
// Most values cluster near the mean, with probability decreasing away from it.
// Use this for:
// - Natural variation (damage ranges, spawn positions with clustering)
// - Measurement errors, particle velocities
// - Anything that should be "usually X but sometimes a bit more/less"
const bj_real mean = BJ_F(0.5) * (bj_real)(GRAPH_W - 1);
const bj_real sd = BJ_F(100.);
for (size_t s = 0; s < distributions[2].n_steps; ++s) {
long x = (long)bj_round(v);
if (x < 0 || x >= (long)GRAPH_W) continue; /* avoid OOB */
size_t y = ++distributions[2].result[(size_t)x];
if (y > distributions[2].max_y) distributions[2].max_y = y;
}
}
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap* bmp) {
uint8_t r, g, b;
bj_make_pixel_rgb(bj_bitmap_mode(bmp), pixel, &r, &g, &b);
r = (uint8_t)(r * factor);
g = (uint8_t)(g * factor);
b = (uint8_t)(b * factor);
return bj_make_bitmap_pixel(bmp, r, g, b);
}
// Visualize distributions as histograms. For each distribution, the x-axis
// represents value buckets and y-axis shows frequency (how many samples fell
// in that bucket). Dots show raw data, smooth curves show moving averages.
// This visualization helps understand how distributions behave statistically.
void draw(bj_bitmap* bmp) {
const uint32_t color_box = bj_make_bitmap_pixel(bmp, 64, 72, 84);
const uint32_t white = bj_make_bitmap_pixel(bmp, 224, 230, 238);
bj_rect graph_box = (bj_rect){
.x = BORDER_W,
.w = GRAPH_W,
.h = GRAPH_H
};
bj_draw_rectangle(bmp, &graph_box, color_box);
for (size_t d = 0; d < N_DISTRIBUTIONS; ++d) {
size_t min_y = distributions[d].min_y; /* 0 for histograms */
size_t max_y = distributions[d].max_y;
double scale = (max_y > min_y)
? (double)(GRAPH_H - 1) / (double)(max_y - min_y)
: 0.0;
uint32_t color_curve = distributions[d].color;
uint32_t color_dots = darken_color(color_curve, 0.7, bmp);
/* dots */
for (int x = 0; x < GRAPH_W; ++x) {
size_t ycount = distributions[d].result[x];
int yscaled = (int)((ycount - min_y) * scale + 0.5);
if (yscaled < 0) yscaled = 0;
if (yscaled > GRAPH_H - 1) yscaled = GRAPH_H - 1;
int sx = graph_box.x + x;
int sy = WINDOW_H - BORDER_H - 1 - yscaled;
bj_put_pixel(bmp, sx, sy, color_dots);
}
/* moving-average curve */
const int W = 21;
int prev_set = 0, px = 0, py = 0;
for (int x = 0; x < GRAPH_W; ++x) {
int half = W / 2;
int xl = (x - half < 0) ? 0 : x - half;
int xr = (x + half >= GRAPH_W) ? GRAPH_W - 1 : x + half;
uint64_t sum = 0;
for (int i = xl; i <= xr; ++i) sum += distributions[d].result[i];
double avg = (double)sum / (double)(xr - xl + 1);
int yscaled = (int)((avg - (double)min_y) * scale + 0.5);
if (yscaled < 0) yscaled = 0;
if (yscaled > GRAPH_H - 1) yscaled = GRAPH_H - 1;
int sx = graph_box.x + x;
int sy = WINDOW_H - BORDER_H - 1 - yscaled;
if (prev_set) bj_draw_line(bmp, px, py, sx, sy, color_curve);
px = sx; py = sy; prev_set = 1;
}
/* legend */
int lx = BORDER_W, ly = 10 + 15 * (int)d;
bmp, &(bj_rect){ .x = lx, .y = ly, .w = 25, .h = 8 }, distributions[d].color);
bj_draw_textf(bmp, lx + 30, ly, 8, white, distributions[d].name, distributions[d].n_steps);
}
bj_draw_textf(bmp, BORDER_W, 10 + 15 * (N_DISTRIBUTIONS + 1), 8, white, "Use Left/Right arrow keys to change number of draws.");
}
static void on_draw(
struct bj_window* w,
struct bj_render_target* target,
const struct bj_rect* dirty,
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
// Force the dark background clear colour every draw. Setting it on
// the window's framebuffer is cheap and keeps roll() ignorant of
// the framebuffer.
draw(fb);
}
static void roll(void) {
}
// Interactive controls: adjust sample count to see how distributions converge.
// With few samples, histograms are noisy. With many samples, they approach
// the theoretical probability curves. This demonstrates the law of large numbers.
void key_callback(bj_window* p_window, const bj_key_event* e, void* data) {
(void)data;
if (e->action != BJ_RELEASE) return;
switch (e->key) {
roll(); break;
if (n_steps_base > 10) { n_steps_base /= 2; roll(); }
break;
if (n_steps_base < 0x20000000) { n_steps_base *= 2; roll(); }
break;
break;
default: break;
}
}
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
bj_quit_app(app, 1);
return 0;
}
window = bj_bind_window("Random Distribution", 100, 100, WINDOW_W, WINDOW_H, 0, 0);
roll();
return 0;
}
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick;
(void)user_data;
bj_quit_app(app, 0);
}
}
static void teardown(struct bj_app* app, void* user_data) {
(void)user_data;
bj_end();
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
return bj_run_app(setup, step, 0, teardown, 0);
}
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
Definition audio_pcm.c:177
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:144
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:170
bj_audio_play_note_data data
Definition audio_pcm.c:104
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
Header file for Bitmap type.
static void on_draw(struct bj_window *w, struct bj_render_target *target, const struct bj_rect *dirty, void *user_data)
Definition bitmap_blit.c:32
#define WINDOW_W
Definition bitmap_blit.c:21
bj_window * window
Definition bitmap_blit.c:24
#define WINDOW_H
Definition bitmap_blit.c:22
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
Definition drawing_2d.c:133
Sytem event management API.
void key_callback(bj_window *p_window, const bj_key_event *e, void *data)
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
Definition app.h:106
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
uint32_t bj_make_bitmap_pixel(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
void bj_draw_textf(struct bj_bitmap *bitmap, int x, int y, unsigned height, uint32_t fg_native, const char *fmt,...)
Prints formatted text into a bitmap, similar to printf.
int bj_bitmap_mode(struct bj_bitmap *bitmap)
Get the pixel mode of the given bitmap.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
void bj_set_bitmap_color(struct bj_bitmap *bitmap, uint32_t color, uint8_t roles)
Sets one or more colour properties of a bitmap.
@ BJ_BITMAP_CLEAR_COLOR
Clear/fill colour for bj_clear_bitmap()
Definition bitmap.h:118
struct bj_render_target bj_render_target
Definition api.h:346
struct bj_bitmap bj_bitmap
Definition api.h:328
struct bj_window bj_window
Definition api.h:354
void bj_draw_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a rectangle in the given bitmap.
void bj_draw_filled_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a filled rectangle in the given bitmap.
void bj_draw_line(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, uint32_t pixel)
Draws a line of pixels in the given bitmap.
enum bj_event_action action
Action (press/release/repeat)
Definition event.h:432
enum bj_key key
Key identifier.
Definition event.h:430
void bj_dispatch_events(void)
Poll and dispatch all pending events.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
@ BJ_KEY_ESCAPE
Esc key.
Definition event.h:176
@ BJ_KEY_LEFT
Left arrow key.
Definition event.h:186
@ BJ_KEY_RIGHT
Right arrow key.
Definition event.h:188
@ BJ_KEY_RETURN
Enter key.
Definition event.h:163
@ BJ_RELEASE
The key or button was released.
Definition event.h:391
Represent a keyboard key event.
Definition event.h:429
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
Definition rect.h:34
#define bj_round
Round to nearest integer.
Definition math.h:245
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:78
float bj_real
Selected real type for float configuration.
Definition math.h:76
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
void bj_memzero(void *dest, size_t mem_size)
Zero out mem_size bytes at dest.
void bj_make_pixel_rgb(enum bj_pixel_mode mode, uint32_t value, uint8_t *red, uint8_t *green, uint8_t *blue)
Gets the RGB value of a pixel given its 32-bits representation.
uint32_t bj_get_pixel_value(enum bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
Definition random.h:173
int32_t bj_uniform_int32_distribution(bj_random_u32_fn next, void *state, int32_t low, int32_t high)
Uniform 32-bit integer in [low, high].
int bj_bernoulli_distribution(bj_random_u32_fn next, void *state, bj_real probability)
Bernoulli(probability).
#define bj_normal_real_distribution
Alias to the real-typed normal distribution for the active precision.
Definition random.h:334
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:81
void bj_set_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
Definition window.h:470
void bj_set_window_should_close(struct bj_window *window)
Flag a given window to be closed.
struct bj_window * bj_bind_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags, struct bj_error **error)
Create a new struct bj_window with the specified attributes.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
bj_bool bj_should_close_window(struct bj_window *window)
Get the close flag state of a window.
void bj_unbind_window(struct bj_window *window)
Deletes a struct bj_window object and releases associated memory.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
#define FB_PIXEL_MODE
Header file for general pixel manipulation facilities.
#define W
Definition pong.c:116
Pseudo-random number generation API.
distribution distributions[3]
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap *bmp)
#define N_DISTRIBUTIONS
static void roll(void)
#define BORDER_W
#define GRAPH_W
#define GRAPH_H
#define BORDER_H
static void run_distributions()
size_t n_steps_base
static void init_distributions(void)
Header file for system interactions.
Header file for bj_window type.