jvasileff / ceylon-random Goto Github PK
View Code? Open in Web Editor NEWRandom Number Generator for Ceylon
License: MIT License
Random Number Generator for Ceylon
License: MIT License
Tests currently fail to detect the deficiency in node's RNG that results in only 32 bits of entropy.
shared native("js")
Random platformRandom() => object
satisfies Random {
value two53 = 2^53;
shared actual
Float nextFloat() {
dynamic {
return Math.random();
}
}
shared actual
Integer nextBits(Integer bits) {
if (bits <= 0) {
return 0;
}
else if (bits <= 53) {
return (nextFloat() * two53).integer / 2^(53-bits);
}
else {
throw Exception(
"bits cannot be greater than \
``randomLimits.maxBits`` on this platform");
}
}
};
shared void run() {
value random = platformRandom();
printAll( { random.nextFloat() * (2.0^53) }.cycled.map((i)
=> formatInteger(i.integer, 2)).take(10), "\n");
}
Produces the output below, but the RNG still passes all tests.
$ ceylon run-js com.vasileff.ceylon.random.api
11011000111101100101101010010100000000000000000000000
10000101010011000111000010100010000000000000000000000
11010010010010101000010110001010000000000000000000000
1011001001101001010010100100011000000000000000000000
1110011010101101011011010101111000000000000000000000
10100110011101011011101011101110000000000000000000000
10100101011111011111001010101011000000000000000000000
10001101110001100010000110010101000000000000000000000
11010000001010100001111100000001000000000000000000000
10101101011100010101101001011011000000000000000000000
A very common use case is to generate more than one random value at a time - a sequence, stream, iteration, whatever. The current API forces you to do something like { random.nextBoolean() }.cycled
or (0:10).map((_) random.nextBoolean())
. It might be nice to provide convenience methods/properties to support random.booleans.take(10)
.
I've added bits(Integer bits)
, integers(Integer bound)
, booleans
, bytes
, floats
, and elements(elements)
, all returning streams. At least for now, they are annotated deprecated("experimental")
.
Hi!
I like your random library and want to contribute some code to it...
But before I get to that, I hope you don't mind if I give some suggestions. For example, I think it's not a good idea to mix this library with your xmath library. The Random library has the potential to become the de-facto random library in Ceylon, but if you start demanding people who only need a silly random generator to get a few things a little less deterministic to download more than one library (and this case it would be a shared import
) this may make it more troublesome than it should be.
My suggestion would be to create some random generators in the xmath
library itself, or even make it fully modular and have a xmath.random
module with random functions that work with xmath
and integrates with random
.
I have some tests that require the generation of random Strings. I wrote it based on Random.elements( characters ).take(n);
.
My test fails quite frequently now... I was using my own poor implementation of random before and it actually never failed.
If the randomness is good, I would expect duplicate Strings only every several billion Strings, but I am using only 100 Strings.
Could you confirm that this Random implementation has some kind of bias (maybe the default seed is very bad or something?).
Here's the implementation of randomStrings()
:
Thanks.
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.