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:
The example visualizes each distribution as a histogram, showing how random samples converge to the expected statistical shape as sample count increases.
#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;
uint32_t color;
size_t min_y;
size_t max_y;
size_t n_steps;
distributions[0].name =
"uniform: %ld draws in [0;Xmax[ ; y = how many x";
distributions[1].name =
"bernoulli: %ld draws with a probability p (x) ; y = how many hits";
distributions[2].name =
"normal: %ld draws with Xmax/2 (mean) and 100 (deviation) ; y = how many x";
}
}
}
for (
size_t px = 0; px <
GRAPH_W; ++px) {
}
}
}
if (x < 0 || x >= (
long)
GRAPH_W)
continue;
}
}
uint8_t r, g, b;
r = (uint8_t)(r * factor);
g = (uint8_t)(g * factor);
b = (uint8_t)(b * factor);
}
};
double scale = (max_y > min_y)
? (
double)(
GRAPH_H - 1) / (
double)(max_y - min_y)
: 0.0;
for (
int x = 0; x <
GRAPH_W; ++x) {
int yscaled = (int)((ycount - min_y) * scale + 0.5);
if (yscaled < 0) yscaled = 0;
int sx = graph_box.
x + x;
}
int prev_set = 0, px = 0, py = 0;
for (
int x = 0; x <
GRAPH_W; ++x) {
int xl = (x - half < 0) ? 0 : 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;
int sx = graph_box.
x + x;
if (prev_set)
bj_draw_line(bmp, px, py, sx, sy, color_curve);
px = sx; py = sy; prev_set = 1;
}
int lx =
BORDER_W, ly = 10 + 15 * (int)d;
}
}
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
}
break;
break;
break;
default: break;
}
}
static void*
setup(
struct bj_app* app,
void* init_data) {
(void)init_data;
return 0;
}
return 0;
}
static void step(
struct bj_app* app,
struct bj_tick_info tick,
void* user_data) {
(void)tick;
(void)user_data;
}
}
static void teardown(
struct bj_app* app,
void* user_data) {
(void)user_data;
}
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
}
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
static void teardown(struct bj_app *app, void *user_data)
bj_audio_play_note_data data
static void * setup(struct bj_app *app, void *init_data)
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)
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
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.
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()
struct bj_render_target bj_render_target
struct bj_bitmap bj_bitmap
struct bj_window bj_window
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)
enum bj_key key
Key identifier.
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_LEFT
Left arrow key.
@ BJ_KEY_RIGHT
Right arrow key.
@ BJ_KEY_RETURN
Enter key.
@ BJ_RELEASE
The key or button was released.
Represent a keyboard key event.
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
#define bj_round
Round to nearest integer.
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
float bj_real
Selected real type for float configuration.
Axis-aligned rectangle: a top-left corner plus a width and height.
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).
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.
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
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.
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.
Header file for general pixel manipulation facilities.
Pseudo-random number generation API.
distribution distributions[3]
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap *bmp)
static void run_distributions()
static void init_distributions(void)
Header file for system interactions.
Header file for bj_window type.