Skip to content

Add an option to select how to seed random() #1080

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

Closed
wants to merge 7 commits into from
Closed

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Jan 27, 2020

This adds an option to asc to select how to seed native math's random() function, currently supporting:

  --seedRandom          Sets the randomness source used by Math.random. Defaults to
                        a compile-time constant while emitting a warning.

                         const     Compile-time constant without a warning.
                         12345     Custom constant without a warning.
                         date      Imports 'Date' from JS and calls Date.now()
                         math      Imports 'Math' from JS and calls Math.random()
                         wasi      Uses the respective WASI interface.
                         ...       Internal name of a custom function returning i64.

@dcodeIO
Copy link
Member Author

dcodeIO commented Jan 27, 2020

This will probably fail somewhere due to randomness in fixtures. Also makes me wonder if we should remove NativeMath.seedRandom because if one uses it explicitly, there'll still be a warning.

import * as JSMath from "./bindings/Math";
import * as JSDate from "./bindings/Date";
import * as wasi from "./bindings/wasi_snapshot";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something wrong here in that "./bindings/wasi" does not work. Can't find the symbols. Thought we had fixed those export * / import * problems :(

@@ -1,4 +1,10 @@
/// <reference path="./util/seedrandom.d.ts" />

import { ArrayBufferView } from "./arraybuffer";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By doing that, we can changetype<ArrayBufferView> anything accessing .dataStart, getting rid of TS syntax highlighting issues.

Comment on lines +249 to +252
let value = Long.fromBits(Math.random() * 0xffffffff, Math.random() * 0xffffffff, true);
assemblyscript.setGlobalAlias(compilerOptions, "ASC_SEEDRANDOM_LOW", (value.low >>> 0).toString());
assemblyscript.setGlobalAlias(compilerOptions, "ASC_SEEDRANDOM_HIGH", (value.high >>> 0).toString());
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems use Long.fromBits here is unnecessary. What about:

const low = Math.random() * 0xffffffff >>> 0;
const high = Math.random() * 0xffffffff >>> 0;
assemblyscript.setGlobalAlias(compilerOptions, "ASC_SEEDRANDOM_LOW", low.toString());
assemblyscript.setGlobalAlias(compilerOptions, "ASC_SEEDRANDOM_HIGH", high.toString());

@dcodeIO
Copy link
Member Author

dcodeIO commented Mar 13, 2020

Superseded by #1159 which adds a programmatic mechanism instead.

@dcodeIO dcodeIO deleted the seedrandom branch March 15, 2020 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants