rtmigo / xrandom_dart Goto Github PK
View Code? Open in Web Editor NEWDart library with random number generators focused on the consistency, performance and reproducibility
Home Page: https://pub.dev/packages/xrandom
License: MIT License
Dart library with random number generators focused on the consistency, performance and reproducibility
Home Page: https://pub.dev/packages/xrandom
License: MIT License
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!
Hello
Could you please add xoshiro256ss pseudorandom generator?
https://xoshiro.di.unimi.it/xoshiro256starstar.c
I understand it is mostly reusing xoshiro256pp but with a slight change.
Thank you in advance!
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:
xrandom_dart/lib/src/21_base32.dart
Lines 83 to 84 in 8b4b47b
nextInt(n)
does not generate uniform random numbers for larger n
.
The issue is with the following line:
xrandom_dart/lib/src/21_base32.dart
Line 88 in 8b4b47b
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');
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.