Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial numbers generated by Util/random.sml have unexpected regularities #256

Closed
2 of 12 tasks
MortenRhiger opened this issue Sep 14, 2022 · 2 comments
Closed
2 of 12 tasks
Assignees
Labels
bug Something isn't working fixed-in-110.99.4 issues that will be fixed in the 110.99.4 version smlnj-lib

Comments

@MortenRhiger
Copy link

MortenRhiger commented Sep 14, 2022

Version

110.99.2

Operating System

  • Any
  • Linux
  • macOS
  • Windows
  • Other Unix

OS Version

No response

Processor

  • Any
  • Arm (using Rosetta)
  • PowerPC
  • Sparc
  • x86 (32-bit)
  • x86-64 (64-bit)
  • Other

System Component

SML/NJ Library

Severity

Minor

Description

The first ca. 1100 numbers (and perhaps more) produced by Random.randInt display unexpected (to me) regularities (see the charts below). The same regularities show up for the few seeds I tried.

Transcript

The numbers produced look almost like a sine wave scaled by the count. The same regularities show up for the few seeds I tried.

Here are the first 1100 numbers (seed (0, 1)):
random1

And here are the first 300:
random2

Expected Behavior

I am aware that a random sequence must contain long streaks of only positives and long streaks of only negatives (and even display sinusoidal behavior from time to time), but I didn't expect to find them so early in the sequence produced by a random-number generator.

If this is a known and desired symptom of the SWB algorithm, then the solution might be to describe it in the documentation.

Steps to Reproduce

val seed = Random.rand(0,1);
fun loop n = if n > 0 then
               ( print (Format.format "%d\n" [Format.INT (Random.randInt seed)]);
                 loop (n - 1) )
             else ();

Running loop 1200 writes the 1200 numbers plotted above.

Additional Information

No response

Email address

morten.rhiger@gmail.com

@MortenRhiger MortenRhiger added the bug Something isn't working label Sep 14, 2022
@JohnReppy JohnReppy self-assigned this Dec 19, 2022
@JohnReppy
Copy link
Contributor

It is probably time to replace the Random implementation with something better. I've ported the 64-bit Mersenne Twister implementation to SML, I just need to wrap it with an interface that is compatible with the RANDOM signature (and port the 32-bit version for 32-bit machines).

@JohnReppy JohnReppy added the fixed-in-110.99.4 issues that will be fixed in the 110.99.4 version label Jan 1, 2023
@JohnReppy
Copy link
Contributor

I have replaced the Random structure with 32-bit and 64-bit versions of the Mersenne Twister implementation. I have tested these against the C reference implementation and they agree (at least for the first 1000 numbers). These new versions will be in the 110.99.4 and 2023.1 releases.

@JohnReppy JohnReppy changed the title Initial numbers generated by Util/random.sml have unexpected regularities Initial numbers generated by Util/random.sml have unexpected regularities Jan 1, 2023
@JohnReppy JohnReppy changed the title Initial numbers generated by Util/random.sml have unexpected regularities Initial numbers generated by Util/random.sml have unexpected regularities Jan 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed-in-110.99.4 issues that will be fixed in the 110.99.4 version smlnj-lib
Projects
None yet
Development

No branches or pull requests

2 participants