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 BJ_AUTOMAIN_CALLBACKS
#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;
}
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;
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;
}
}
}
break;
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;
}
return bj_callback_continue;
}
(void)user_data;
? bj_callback_exit_success
: bj_callback_continue;
}
(void)user_data;
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.