-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Adds random number generation for BigInts and BigFloats #4845
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f72e0a0
Adds random number generation for BigInts from GMP
jiahao 678df94
Refactor BigInt RNG
andrioni 6b53cfc
Add a default BigRNG and the standard interface to seed BigRNGs
andrioni 2147bca
Add BigFloat random number generation
andrioni 3549bb8
Improved support for random BigInt ranges
andrioni 7ef6733
Add normally-distributed BigFloats generation
andrioni bf8c452
srand() now seeds both RNGs
andrioni 5cb8606
Use `shasum` instead of `sha1sum` in OS X
andrioni 4bf4829
Add documentation to the BigRNG
andrioni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ export | |
Bidiagonal, | ||
BigFloat, | ||
BigInt, | ||
BigRNG, | ||
BitArray, | ||
BitMatrix, | ||
BitVector, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
module GMP | ||
|
||
export BigInt | ||
export BigInt, BigRNG | ||
|
||
import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), ($), | ||
binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod, | ||
ndigits, promote_rule, rem, show, isqrt, string, isprime, powermod, | ||
widemul, sum, trailing_zeros, trailing_ones, count_ones, base, parseint, | ||
serialize, deserialize, bin, oct, dec, hex, isequal, invmod, | ||
prevpow2, nextpow2 | ||
prevpow2, nextpow2, rand, rand!, srand | ||
|
||
type BigInt <: Integer | ||
alloc::Cint | ||
|
@@ -421,4 +421,87 @@ widemul{T<:Integer}(x::T, y::T) = BigInt(x)*BigInt(y) | |
prevpow2(x::BigInt) = x < 0 ? -prevpow2(-x) : (x <= 2 ? x : one(BigInt) << (ndigits(x, 2)-1)) | ||
nextpow2(x::BigInt) = x < 0 ? -nextpow2(-x) : (x <= 2 ? x : one(BigInt) << ndigits(x-1, 2)) | ||
|
||
# Random number generation | ||
type BigRNG <: AbstractRNG | ||
seed_alloc::Cint | ||
seed_size::Cint | ||
seed_d::Ptr{Void} | ||
alg::Cint | ||
alg_data::Ptr{Void} | ||
|
||
function BigRNG() | ||
randstate = new(zero(Cint), zero(Cint), C_NULL, zero(Cint), C_NULL) | ||
# The default algorithm in GMP is currently MersenneTwister | ||
ccall((:__gmp_randinit_default, :libgmp), Void, (Ptr{BigRNG},), | ||
&randstate) | ||
finalizer(randstate, BigRNG_clear) | ||
randstate | ||
end | ||
end | ||
|
||
# Initialize with seed | ||
function BigRNG(seed::Uint64) | ||
randstate = BigRNG() | ||
ccall((:__gmp_randseed_ui, :libgmp), Void, (Ptr{BigRNG}, Culong), | ||
&randstate, seed) | ||
randstate | ||
end | ||
function BigRNG(seed::BigInt) | ||
randstate = BigRNG() | ||
ccall((:__gmp_randseed, :libgmp), Void, (Ptr{BigRNG}, Ptr{BigInt}), | ||
&randstate, &seed) | ||
randstate | ||
end | ||
|
||
function BigRNG_clear(x::BigRNG) | ||
ccall((:__gmp_randclear, :libgmp), Void, (Ptr{BigRNG},), &x) | ||
end | ||
|
||
function rand(::Type{BigInt}, randstate::BigRNG, n::Integer) | ||
m = convert(BigInt, n) | ||
randu(randstate, m) | ||
end | ||
|
||
function rand(r::Range1{BigInt}) | ||
ulen = convert(BigInt, length(r)) | ||
convert(BigInt, first(r) + randu(ulen)) | ||
end | ||
|
||
function rand!(r::Range1{BigInt}, A::Array{BigInt}) | ||
for i = 1:length(A) | ||
A[i] = rand(r) | ||
end | ||
A | ||
end | ||
|
||
rand(r::Range1{BigInt}, dims::Dims) = rand!(r, Array(BigInt, dims)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also redundant. |
||
rand(r::Range1{BigInt}, dims::Int...) = rand!(r, Array(BigInt, dims...)) | ||
|
||
function randu(randstate::BigRNG, k::BigInt) | ||
z = BigInt() | ||
ccall((:__gmpz_urandomm, :libgmp), Void, | ||
(Ptr{BigInt}, Ptr{BigRNG}, Ptr{BigInt}), &z, &randstate, &k) | ||
z | ||
end | ||
randu(k::BigInt) = randu(Base.Random.DEFAULT_BIGRNG, k) | ||
|
||
srand(r::BigRNG, seed) = srand(r, convert(BigInt, seed)) | ||
function srand(randstate::BigRNG, seed::Vector{Uint32}) | ||
s = big(0) | ||
for i in seed | ||
s = s << 32 + i | ||
end | ||
srand(randstate, s) | ||
end | ||
function srand(randstate::BigRNG, seed::Uint64) | ||
ccall((:__gmp_randseed_ui, :libgmp), Void, (Ptr{BigRNG}, Culong), | ||
&randstate, seed) | ||
return | ||
end | ||
function srand(randstate::BigRNG, seed::BigInt) | ||
ccall((:__gmp_randseed, :libgmp), Void, (Ptr{BigRNG}, Ptr{BigInt}), | ||
&randstate, &seed) | ||
return | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is redundant with definitions in
random.jl