Giter Site home page Giter Site logo

rtmigo / xrandom_dart Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 10.72 MB

Dart library with random number generators focused on the consistency, performance and reproducibility

Home Page: https://pub.dev/packages/xrandom

License: MIT License

Shell 0.06% Dart 99.94%
random random-number-generators random-generation flutter xorshift xorshift-generator xoshiro dart dart-library dart-package

xrandom_dart's Issues

Int is not a true 64 bit - other type needed

Hello

The four seed parameters defined for Xoshiro256ss are int each.
However, in dart the int type is only 2^63-1 max value, because 1 bit is used for defining the sign (https://stackoverflow.com/a/53591126/346488).
Which means int is not a proper type to use as an input, as it is not possible to pass each part as a true 64-bit integer.

E.g. I have an issue passing one as:
int.parse('f78207dec08eba34', radix:16)
It returns an error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Positive input exceeds the limit of integer

It looks like the right approach would be to use BigInt as input parameters type and then check inside that each parameter is within 64 bits.

Could you please check on this?
Thank you!

Limit for max in nextInt should be 2^31 inclusive, not exclusive

If nextInt(1 << 32) is called on Xrandom() a RangeError is thrown. The same call on the dart Random() works fine because the limit for max is defined as 1 << 32 inclusively. The generated values will of course be in the range from 0 (inclusive) to 1 << 32 exclusive, so exactly 32 random bits.

The Problem is in the lines:

if (max < 1 || max > 0xFFFFFFFF) {
throw RangeError.range(max, 1, 0xFFFFFFFF);

nextInt is not uniform

nextInt(n) does not generate uniform random numbers for larger n.

The issue is with the following line:

for (int u = r; u - (r = u % max) + m < 0; u = nextRaw32()) {}

If I'm not mistaken the line should be for (int u = r; u - (r = u % max) + max > (1 << 32); u = nextRaw32()) {}. Not sure what the point of m = max-1 is, but i don't think its needed.

Since u % max is always <= u, u-(u%max) is never negative. And since m is also never negative, u-(r=u%max)+m will never be less than 0. So essentially only nextRaw32() % max will every be returned. This is approximately uniform for small m but for larger m, it is not. This can easily experimentally be verified.

The following program prints lower: 6665204, upper: 3334796 even though there should be approximately an equal amout of numers below the midpoint as above.

import 'package:xrandom/xrandom.dart';

void main() {
  final random = Xrandom(42);

  const mid = (1 << 32) ~/ 3;
  const max = mid * 2;
  var lower = 0;
  var upper = 0;
  for (var i = 0; i < 10000000; i++) {
    if (random.nextInt(max) < mid) {
      lower++;
    } else {
      upper++;
    }
  }
  print('lower: $lower, upper: $upper');
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.