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

AGS 4: Script API: add eRound options and new Maths.Random() #2684

Open
wants to merge 3 commits into
base: ags4
Choose a base branch
from

Conversation

ivan-mogilko
Copy link
Contributor

@ivan-mogilko ivan-mogilko commented Feb 18, 2025

Resolve #1983, #2683

Contents:

  1. Expands eRound option with 2 new modes:

    • eRoundTowardsZero - rounds towards a closest integer less by magnitude (absolute value);
    • eRoundAwayFromZero - rounds towards a closest integer larger by magnitude (absolute value);
  2. Add float Maths.Round(float, RoundDirection) - which rounds a float value to another float value (without making it int).

  3. Add Maths.Random(limit), a proper variant of Random, which treats limit as an upper exclusive boundary. So Maths.Random(10) would choose a number between 0 and 9, but never 10.

  4. Add Maths.RandomFloat() which produces a float in range [0.0, 1.0) (excluding 1.0).

@ivan-mogilko ivan-mogilko added ags 4 related to the ags4 development context: script api labels Feb 18, 2025
@ivan-mogilko ivan-mogilko changed the base branch from master to ags4 February 18, 2025 23:27
@AlanDrake
Copy link
Contributor

AlanDrake commented Feb 19, 2025

I've never seen a round direction in random algorithms, but I can see it could come in handy. sorry, I misread

The only weakness is relying on rand(), we should perhaps implement a xoshiro/xoroshiro/lehmer/PCG algorithm, which are fast and have better statistical distributions (could be another PR).

@messengerbag
Copy link

Thanks, that's grand, @ivan-mogilko!

Only thing is Maths.RandomFloat(), which – just as with Maths.Random() – I think should exclude the upper limit, i.e. [0.0, 1.0). This would be consistent with Math.random() in Java and Javascript, C#'s Random.NextDouble(), Python's random.random(), etc.

@ivan-mogilko
Copy link
Contributor Author

Only thing is Maths.RandomFloat(), which – just as with Maths.Random() – I think should exclude the upper limit, i.e. [0.0, 1.0).

I was not sure if it should or not, but forgot to check existing examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ags 4 related to the ags4 development context: script api
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Script API: add a equivalent of "truncate" to FloatToInt modes
3 participants