Files
libcdsb/tests/src/random.c
T

112 lines
3.1 KiB
C
Raw Normal View History

2022-06-02 21:41:06 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <time.h>
#include <stdlib.h>
#include "../include/test.h"
#include "../include/random.h"
static void init(unsigned int seed) {
2022-06-03 11:54:22 +03:00
if (!seed) seed = time(0);
2022-06-02 21:41:06 +03:00
printf("\e[36mRandom module initialized with seed: \e[m\e[32m%u\n\e[m", seed);
put_separator();
srand(seed);
}
int random_init(int argc, char** argv) {
if (argc < 2) {
2022-06-03 11:54:22 +03:00
init(0);
2022-06-02 21:41:06 +03:00
} else init(atol(argv[1]));
return 0;
}
vtype_bool random_boolean() {
return rand()&1;
}
vtype_float random_float() {
if (rand()&1) {
return ((vtype_float)random_uint16() / (vtype_float)random_int16()) * -1;
}
return (vtype_float)random_uint16() / (vtype_float)random_int16();
}
vtype_double random_double() {
if (rand()&1) {
return ((vtype_double)random_uint32() / (vtype_double)random_int32()) * -1;
}
return (vtype_double)random_uint32() / (vtype_double)random_int32();
}
vtype_ldouble random_ldouble() {
if (rand()&1) {
return ((vtype_ldouble)random_uint64() / (vtype_ldouble)random_int64()) * -1;
}
return (vtype_ldouble)random_uint64() / (vtype_ldouble)random_int64();
}
vtype_int8 random_int8() {
return random_uint8();
}
vtype_int16 random_int16() {
return random_uint16();
}
vtype_int32 random_int32() {
return rand() | ((rand()&1) << 31);
}
vtype_uint8 random_uint8() {
return rand() % 0x100;
}
vtype_uint16 random_uint16() {
return rand() % 0x10000;
}
vtype_uint32 random_uint32() {
2022-06-02 21:48:56 +03:00
return (rand()<<1) | (rand()&1);
2022-06-02 21:41:06 +03:00
}
vtype_uint64 random_uint64() {
return random_int32() * random_int32();
}
vtype_int64 random_int64() {
return random_uint32() * random_uint32();
}
2022-06-03 19:29:06 +03:00
char random_ascii_char() {
return (rand()%0x5f) + 0x20;
}
unsigned int random_unicode_symbol() {
switch (rand()%20) {
default:
case 0: return (random_uint16()%0x005f) + 0x000020;
case 1: return (random_uint16()%0x020f) + 0x0000a1;
case 2: return (random_uint16()%0x01a8) + 0x000388;
case 3: return (random_uint16()%0x0026) + 0x000531;
case 4: return (random_uint16()%0x002a) + 0x000560;
case 5: return (random_uint16()%0x00c6) + 0x001000;
case 6: return (random_uint16()%0x0030) + 0x0010d0;
case 7: return (random_uint16()%0x029d) + 0x001400;
case 8: return (random_uint16()%0x0058) + 0x0016a0;
case 9: return (random_uint16()%0x0074) + 0x001b80;
case 10: return (random_uint16()%0x00f4) + 0x001d00;
case 11: return (random_uint16()%0x0115) + 0x001e00;
case 12: return (random_uint16()%0x008b) + 0x002100;
case 13: return (random_uint16()%0x0297) + 0x002190;
case 14: return (random_uint16()%0x0714) + 0x002460;
case 15: return (random_uint16()%0x00b7) + 0x00a640;
case 16: return (random_uint16()%0x0074) + 0x00a8e0;
case 17: return (random_uint16()%0x009e) + 0x010400;
case 18: return (random_uint16()%0x0137) + 0x010600;
case 19: return (random_uint16()%0x03d8) + 0x01f300;
}
}