Skip to content

Conversation

@MineCake147E
Copy link
Contributor

What

#731 を実装した際、Float64ArrayUint8Array() constructorの第一引数に指定した場合の仕様の理解が間違っており、numで与えられたシード値が意図せず& 255されてしまっていました。
本修正では、TypedArray.prototype.bufferを取る事により、#731 の実装時に意図したように、numberの内部表現をそのままseedとして使用することにより、3.03.0000000000000004で異なる出力を得られるようになります。
これにより、全く同じseedでも、修正前と修正後で出力が変わります。
また、本修正後も旧来の動作を再現できるよう、関数Math:gen_rngoptions.chacha20NumberSeedLegacyBehaviour(デフォルト:false)にtrueを指定した場合、修正前の動作をする機能を追加しました。

<: (Math:gen_rng(0)(0, 4503599627370495)).to_hex()
<: (Math:gen_rng(0.000001)(0, 4503599627370495)).to_hex()
<: (Math:gen_rng(256.000001)(0, 4503599627370495)).to_hex()

Before:

"b3f8c68b3d7e0"
"b3f8c68b3d7e0"
"b3f8c68b3d7e0"
(null)

After:

"53b92953000f2"
"29f33b8f9931b"
"1ed54e7a15574"
(null)

Why

某Playの出力が複数日連続で同一となった事により発覚。
algorithmrc4を指定した場合と挙動が大きく異なるため。

Additional info (optional)

Workaround for versions before this fix

  • seedを与える前に.to_str()等でstringに変換する

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/utils/random/genrng.ts 80.00% 2 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Files with missing lines Coverage Δ
src/interpreter/lib/math.ts 64.64% <100.00%> (ø)
src/utils/random/genrng.ts 57.44% <80.00%> (+5.36%) ⬆️

... and 29 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +2 to +3
- 関数`Math:gen_rng``options.chacha20NumberSeedLegacyBehaviour``true`を指定した場合、修正前の動作をする機能を追加(デフォルト:`false`)。
- これらの修正により、同じ`seed`でも修正前と修正後で生成される値が異なるようになります。
Copy link
Member

Choose a reason for hiding this comment

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

CHANGELOGは基本的にインデントにタブ文字を使っているので一応変更を提案しておきます

Suggested change
- 関数`Math:gen_rng``options.chacha20NumberSeedLegacyBehaviour``true`を指定した場合、修正前の動作をする機能を追加(デフォルト:`false`)。
- これらの修正により、同じ`seed`でも修正前と修正後で生成される値が異なるようになります。
- 関数`Math:gen_rng`の`options.chacha20NumberSeedLegacyBehaviour`に`true`を指定した場合、修正前の動作をする機能を追加(デフォルト:`false`)。
- これらの修正により、同じ`seed`でも修正前と修正後で生成される値が異なるようになります。

actualSeed = new Uint8Array(await crypto.subtle.digest('SHA-384', new Uint8Array(new Float64Array([seed.value]))));
if (seed.type === 'num') {
const float64Array = new Float64Array([seed.value]);
const numberAsIntegerOptionValue = options?.get('chacha20NumberSeedLegacyBehaviour');
Copy link
Member

Choose a reason for hiding this comment

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

多くのプログラミング言語の標準ライブラリで米式スペルを用いているのを見るのでBehaviorの方がいいかもしれません。
それと、AiScriptでは組み込みプロパティなどにスネークケースを使っているのでそれに合わせたほうが統一感がありそうです。

Suggested change
const numberAsIntegerOptionValue = options?.get('chacha20NumberSeedLegacyBehaviour');
const numberAsIntegerOptionValue = options?.get('chacha20_number_seed_legacy_behavior');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants