Skip to content

Commit 22f2973

Browse files
committed
Implement a pseudo random number generator for RND/RANDOMIZE
1 parent a02f50d commit 22f2973

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tcbasic_SOURCES = \
3232
line.h line.c \
3333
mulop.h mulop.c \
3434
number.h number.c \
35+
prng.h prng.c \
3536
rem.h rem.c \
3637
relop.h relop.c \
3738
rnd.h rnd.c \

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ AC_PROG_CC
2626
AC_HEADER_STDC
2727

2828
AC_CHECK_HEADERS([getopt.h])
29-
AC_CHECK_FUNCS([arc4random arc4random_uniform getopt_long strsep])
29+
AC_CHECK_FUNCS([getopt_long strsep])
3030

3131
AC_CONFIG_HEADERS([config.h:config.in])
3232

prng.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
tcbasic - a small BASIC Interpreter written in C.
3+
Copyright (C) 2015 Thomas Cort <linuxgeek@gmail.com>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
static unsigned long tcb_seed = 1;
21+
22+
#define A 16807
23+
#define M 2147483647
24+
25+
void tcb_srand(unsigned long seed) {
26+
tcb_seed = (seed == 0) ? 1 : seed;
27+
}
28+
29+
float tcb_rand(void) {
30+
tcb_seed = (A * tcb_seed) % M;
31+
return tcb_seed / (float) M;
32+
}

prng.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
tcbasic - a small BASIC Interpreter written in C.
3+
Copyright (C) 2015 Thomas Cort <linuxgeek@gmail.com>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef __PRNG_H
20+
#define __PRNG_H
21+
22+
void tcb_srand(unsigned long seed);
23+
float tcb_rand(void);
24+
25+
#endif

rnd.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdlib.h>
2222
#include <string.h>
2323

24+
#include "prng.h"
2425
#include "tokenizer.h"
2526

2627
#include "number.h"
@@ -52,10 +53,7 @@ struct rnd *parse_rnd(struct tokenizer *t) {
5253
}
5354

5455
struct number *eval_rnd(struct rnd *r) {
55-
56-
/* IEEE 754 floating point -- exponent is 2^-1, mantissa is "random" */
57-
int x = 0x3f000000 | (rand() & 0x7FFFFF);
58-
return new_number_from_float(*((float*)&x));
56+
return new_number_from_float(tcb_rand());
5957
}
6058

6159
void print_rnd(struct rnd *r) {

statement.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <time.h>
2424

2525
#include "buffer.h"
26+
#include "prng.h"
2627
#include "readaline.h"
2728
#include "runtime.h"
2829
#include "tokenizer.h"
@@ -306,7 +307,7 @@ int eval_statement(struct statement *s, int number, int next_number) {
306307
case REM:
307308
break;
308309
case RANDOMIZE:
309-
srand(time(NULL));
310+
tcb_srand(time(NULL));
310311
break;
311312
}
312313

0 commit comments

Comments
 (0)