Banjo API 0.0.1
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.

#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/bitmap.h>
#include <banjo/draw.h>
#include <banjo/event.h>
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/pixel.h>
#include <banjo/random.h>
#include <banjo/renderer.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
#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(bj_bitmap* bmp) {
distributions[0].name = "uniform: %ld draws in [0;Xmax[ ; y = how many x";
distributions[0].color = bj_make_bitmap_pixel(bmp, 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_make_bitmap_pixel(bmp, 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_make_bitmap_pixel(bmp, 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 roll() {
}
// 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;
}
}
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
return bj_callback_exit_error;
}
window = bj_bind_window("Random Distribution", 100, 100, WINDOW_W, WINDOW_H, 0, 0);
roll();
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
bj_sleep(30);
? bj_callback_exit_success
: bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
bj_end();
return status;
}
int bj_app_begin(void **user_data, int argc, char *argv[])
Definition audio_pcm.c:25
int bj_app_iterate(void *user_data)
Definition audio_pcm.c:60
int bj_app_end(void *user_data, int status)
Definition audio_pcm.c:84
bj_audio_play_note_data data
Definition audio_pcm.c:22
Header file for Bitmap type.
bj_renderer * renderer
Definition bitmap_blit.c:25
#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.
Sytem event management API.
void key_callback(bj_window *p_window, const bj_key_event *e, void *data)
Definition event_callbacks.c:49
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear color.
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 color, 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 color at given coordinate.
void bj_set_bitmap_color(struct bj_bitmap *bitmap, uint32_t color, uint8_t roles)
Sets one or more color properties of a bitmap.
@ BJ_BITMAP_CLEAR_COLOR
Clear/fill color for bj_clear_bitmap()
Definition bitmap.h:74
struct bj_bitmap bj_bitmap
Definition api.h:270
struct bj_renderer bj_renderer
Definition api.h:288
struct bj_window bj_window
Definition api.h:296
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:339
enum bj_key key
Key identifier.
Definition event.h:337
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:83
@ BJ_KEY_LEFT
Left arrow key.
Definition event.h:93
@ BJ_KEY_RIGHT
Right arrow key.
Definition event.h:95
@ BJ_KEY_RETURN
Enter key.
Definition event.h:70
@ BJ_RELEASE
The key or button was released.
Definition event.h:298
Represent a keyboard key event.
Definition event.h:336
int16_t x
The x-coordinate of the rectangle's top-left corner.
Definition rect.h:20
#define bj_round
Round to nearest integer.
Definition math.h:220
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
float bj_real
Selected real type for float configuration.
Definition math.h:51
Represents a rectangle with position and dimensions.
Definition rect.h:19
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.
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
Definition random.h:105
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:266
void bj_present(struct bj_renderer *renderer, struct bj_window *window)
Present the framebuffer to a window.
struct bj_renderer * bj_create_renderer(enum bj_renderer_type type, struct bj_error **error)
Create a new renderer instance.
struct bj_bitmap * bj_get_framebuffer(struct bj_renderer *renderer)
Get the renderer's framebuffer.
bj_bool bj_renderer_configure(struct bj_renderer *renderer, struct bj_window *window, struct bj_error **error)
Configure a renderer for a specific window.
void bj_destroy_renderer(struct bj_renderer *renderer)
Destroy a renderer and free associated resources.
@ BJ_RENDERER_TYPE_SOFTWARE
Software (CPU-based) renderer.
Definition renderer.h:35
bj_bool bj_begin(int systems, struct bj_error **error)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:20
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
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.
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 substitution and application callback facilities.
static void draw()
Definition physics_kinematics.c:136
bj_bitmap * framebuffer
Definition physics_kinematics.c:35
Header file for general pixel manipulation facilities.
Pseudo-random number generation API.
distribution distributions[3]
Definition random_distribution.c:49
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap *bmp)
Definition random_distribution.c:128
#define N_DISTRIBUTIONS
Definition random_distribution.c:36
#define BORDER_W
Definition random_distribution.c:30
#define GRAPH_W
Definition random_distribution.c:33
#define GRAPH_H
Definition random_distribution.c:34
#define BORDER_H
Definition random_distribution.c:31
static void run_distributions()
Definition random_distribution.c:71
size_t n_steps_base
Definition random_distribution.c:47
static void roll()
Definition random_distribution.c:206
static void init_distributions(bj_bitmap *bmp)
Definition random_distribution.c:54
Definition random_distribution.c:38
Rendering backend interface.
Header file for system interactions.
Header file for time manipulation utilities.
Header file for bj_window type.