Implemented rand() and srand()

This commit is contained in:
Max Wash
2021-01-03 11:52:47 +00:00
parent d397eac2d9
commit 92250befd3
2 changed files with 34 additions and 0 deletions

View File

@@ -14,6 +14,9 @@ extern void *realloc(void *ptr, size_t sz);
extern void *calloc(size_t n, size_t sz);
extern void free(void *ptr);
extern int rand(void);
extern void srand(unsigned int seed);
extern int atoi(const char *str);
extern double atof(const char *str);
extern long int strtol(const char *str, char **endptr, int base);

31
photon/libc/stdlib/rand.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stddef.h>
static unsigned int seed = 0;
void srand(unsigned int _seed)
{
seed = _seed;
}
int rand(void)
{
int next = seed;
int result;
next *= 1103515245;
next += 12345;
result = (int)(next / 65536) % 2048;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (int)(next / 65536) % 1024;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (int)(next / 65536) % 1024;
seed = next;
return result;
}