Implemented rand() and srand()
This commit is contained in:
@@ -14,6 +14,9 @@ extern void *realloc(void *ptr, size_t sz);
|
|||||||
extern void *calloc(size_t n, size_t sz);
|
extern void *calloc(size_t n, size_t sz);
|
||||||
extern void free(void *ptr);
|
extern void free(void *ptr);
|
||||||
|
|
||||||
|
extern int rand(void);
|
||||||
|
extern void srand(unsigned int seed);
|
||||||
|
|
||||||
extern int atoi(const char *str);
|
extern int atoi(const char *str);
|
||||||
extern double atof(const char *str);
|
extern double atof(const char *str);
|
||||||
extern long int strtol(const char *str, char **endptr, int base);
|
extern long int strtol(const char *str, char **endptr, int base);
|
||||||
|
|||||||
31
photon/libc/stdlib/rand.c
Normal file
31
photon/libc/stdlib/rand.c
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user