Skip to content

Math:gen_rngのシードがnullを受け付ける #1012

Description

@poppingmoon

Math:gen_rng のアルゴリズムがchacha20のとき、 seednull にすることができ、シードがランダムな乱数生成器が生成される。

if (seed.type !== 'num' && seed.type !== 'str' && seed.type !== 'null') throw new AiScriptRuntimeError('`seed` must be either number or string if specified.');

if (!seed || seed.type !== 'num' && seed.type !== 'str' && seed.type !== 'null') return NULL;
let actualSeed : Uint8Array | undefined = undefined;
if (seed.type === 'num')
{
actualSeed = new Uint8Array(await crypto.subtle.digest('SHA-384', new Uint8Array(new Float64Array([seed.value]))));
} else if (seed.type === 'str') {
actualSeed = new Uint8Array(await crypto.subtle.digest('SHA-384', new Uint8Array(textEncoder.encode(seed.value))));
}
const rng = new ChaCha20(actualSeed);

if (typeof seed === 'undefined') {
keynonce = crypto.getRandomValues(new Uint8Array(keyNonceBytes));
} else {

他のアルゴリズムの場合も seednull にすることができるが、エラーは発生せず null が返される。

if (!seed || seed.type !== 'num' && seed.type !== 'str') return NULL;
const rng = seedrandom(seed.value.toString());

if (!seed || seed.type !== 'num' && seed.type !== 'str') return NULL;
const rng = new SeedRandomWrapper(seed.value);

<: Math:gen_rng(null)()
<: Math:gen_rng(null)()

<: Math:gen_rng(null, {algorithm: "rc4_legacy"})
<: Math:gen_rng(null, {algorithm: "rc4"})

Playground

出力

0.21390153769277456
0.15832915539986403
null
null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions