File tree Expand file tree Collapse file tree 7 files changed +50
-6
lines changed
samples/distro-examples/tests Expand file tree Collapse file tree 7 files changed +50
-6
lines changed Original file line number Diff line number Diff line change @@ -198,7 +198,7 @@ print "RIGHT:" + RIGHT (s,2)
198
198
print " RIGHTOF:" + RIGHTOF (s1 , s2 )
199
199
print " RIGHTOFLAST:" + RIGHTOFLAST (s1 , s2 )
200
200
print " RINSTR:" + RINSTR (2 , s1 , s2 )
201
- print " RND:" + RND
201
+ print " RND:" + iff ( RND >= 0 && RND <= 1.0 , 1 , 0 )
202
202
print " ROUND:" + ROUND (x ,22 )
203
203
print " RTRIM:" + RTRIM (s )
204
204
print " RUN:" ' + RUN cmdstr
Original file line number Diff line number Diff line change @@ -183,7 +183,7 @@ RIGHT:gs
183
183
RIGHTOF:
184
184
RIGHTOFLAST:
185
185
RINSTR:0
186
- RND:0.75898406142369
186
+ RND:1
187
187
ROUND:12.3
188
188
RTRIM:catsanddogs
189
189
RUN:
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ libsb_common_a_SOURCES = \
54
54
device.c device.h \
55
55
screen.c \
56
56
system.c \
57
+ random.c \
57
58
eval.c \
58
59
extlib.c extlib.h \
59
60
file.c \
Original file line number Diff line number Diff line change @@ -2051,18 +2051,18 @@ void cmd_restore() {
2051
2051
* RANDOMIZE [num]
2052
2052
*/
2053
2053
void cmd_randomize () {
2054
- long seed ;
2054
+ var_int_t seed ;
2055
2055
2056
2056
byte code = code_peek ();
2057
2057
switch (code ) {
2058
2058
case kwTYPE_LINE :
2059
2059
case kwTYPE_EOC :
2060
- srand (clock ());
2060
+ pcg32_srand (clock ());
2061
2061
break ;
2062
2062
default :
2063
2063
seed = par_getint ();
2064
2064
if (!prog_error ) {
2065
- srand (seed );
2065
+ pcg32_srand (seed );
2066
2066
}
2067
2067
};
2068
2068
}
Original file line number Diff line number Diff line change @@ -480,7 +480,7 @@ var_num_t cmd_math0(long funcCode) {
480
480
}
481
481
break ;
482
482
case kwRND :
483
- r = (( var_num_t ) rand ()) / ( RAND_MAX + 1.0 );
483
+ r = pcg32_rand ( );
484
484
break ;
485
485
default :
486
486
rt_raise ("Unsupported built-in function call %ld" , funcCode );
Original file line number Diff line number Diff line change
1
+ // This file is part of SmallBASIC
2
+ //
3
+ // lowlevel device (OS) I/O
4
+ //
5
+ // This program is distributed under the terms of the GPL v2.0 or later
6
+ // Download the GNU Public License (GPL) from www.gnu.org
7
+ //
8
+ // Copyright(C) 2000 Nicholas Christopoulos
9
+
10
+ #include "config.h"
11
+
12
+ #include "common/sys.h"
13
+ #include <stdint.h>
14
+ #include <limits.h>
15
+
16
+ // see:
17
+ // https://en.wikipedia.org/wiki/Permuted_congruential_generator
18
+ // https://www.pcg-random.org/download.html
19
+
20
+ static uint64_t state = 0x4d595df4d0f33173 ;
21
+ static uint64_t multiplier = 6364136223846793005u ;
22
+ static uint64_t increment = 1442695040888963407u ;
23
+
24
+ static uint32_t rotr32 (uint32_t x , unsigned r ) {
25
+ return x >> r | x << (- r & 31 );
26
+ }
27
+
28
+ var_num_t pcg32_rand () {
29
+ uint64_t x = state ;
30
+ unsigned count = (unsigned )(x >> 59 );
31
+ state = x * multiplier + increment ;
32
+ x ^= x >> 18 ;
33
+ int32_t r = rotr32 ((uint32_t )(x >> 27 ), count );
34
+ return ((var_num_t )abs (r )) / (INT_MAX + 1.0 );
35
+ }
36
+
37
+ void pcg32_srand (var_int_t seed ) {
38
+ state = seed + increment ;
39
+ pcg32_rand ();
40
+ }
Original file line number Diff line number Diff line change @@ -120,6 +120,9 @@ typedef uint32_t bcip_t;
120
120
#include "include/var.h"
121
121
#include "common/str.h"
122
122
123
+ var_num_t pcg32_rand (void );
124
+ void pcg32_srand (var_int_t seed );
125
+
123
126
#if !defined(O_BINARY )
124
127
#define O_BINARY 0
125
128
#endif
You can’t perform that action at this time.
0 commit comments