diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
| commit | ac55496d881e0a17b3eff85f1faae5aafbc53b50 (patch) | |
| tree | 65b954e994d7bbfc76bc959876e28f7ea9fa708c /random.h | |
| download | data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.gz data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.bz2 data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.zip | |
erster Commit
Diffstat (limited to 'random.h')
| -rw-r--r-- | random.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/random.h b/random.h new file mode 100644 index 0000000..9094d5b --- /dev/null +++ b/random.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #ifndef ITS1_RANDOM_H_INCLUDED | ||
| 2 | #define ITS1_RANDOM_H_INCLUDED | ||
| 3 | |||
| 4 | #ifdef __cplusplus | ||
| 5 | extern "C" { | ||
| 6 | #endif | ||
| 7 | |||
| 8 | struct random_ctx { | ||
| 9 | unsigned char (*get_byte)(struct random_ctx *ctx); | ||
| 10 | }; | ||
| 11 | |||
| 12 | static unsigned char | ||
| 13 | random_get_byte(struct random_ctx *ctx) | ||
| 14 | { | ||
| 15 | return ctx->get_byte(ctx); | ||
| 16 | } | ||
| 17 | |||
| 18 | static unsigned short | ||
| 19 | random_get_word(struct random_ctx *ctx) | ||
| 20 | { | ||
| 21 | return (ctx->get_byte(ctx) << 8) | ||
| 22 | | ctx->get_byte(ctx) | ||
| 23 | ; | ||
| 24 | } | ||
| 25 | |||
| 26 | static unsigned long | ||
| 27 | random_get_dword(struct random_ctx *ctx) | ||
| 28 | { | ||
| 29 | return (ctx->get_byte(ctx) << 24) | ||
| 30 | | (ctx->get_byte(ctx) << 16) | ||
| 31 | | (ctx->get_byte(ctx) << 8) | ||
| 32 | | ctx->get_byte(ctx) | ||
| 33 | ; | ||
| 34 | } | ||
| 35 | |||
| 36 | static unsigned long | ||
| 37 | random_get_dword_range(struct random_ctx *ctx, unsigned long l, unsigned long u) | ||
| 38 | { | ||
| 39 | return l + random_get_dword(ctx) % (u - l + 1); | ||
| 40 | } | ||
| 41 | |||
| 42 | #ifdef __cplusplus | ||
| 43 | } | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #endif /* ITS1_RANDOM_H_INCLUDED */ | ||
| 47 | |||
