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

RFC: Adds random number generation for BigInts from GMP #3077

Closed
wants to merge 1 commit into from

Conversation

jiahao
Copy link
Member

@jiahao jiahao commented May 11, 2013

Introduces new type BigIntRand that initializes GMP RNG.

  • gmp_randinit_default - initialize RNG (current default: Mersenne Twister)
  • gmp_randseed_ui, gmp_randseed - sets random seed

Provides methods to generate random BigInts.

  • rand(Range1{BigInt}) - wraps mpz_urandomm
  • rand!(Range1, Array) - initializes a BigIntRand automatically and populates an array

Also adds low-level creation of BigInttype from constituent data elements.

Is this the right way to expose the RNG state from GMP?

@jiahao
Copy link
Member Author

jiahao commented May 11, 2013

cc: @andrioni - This is as far as I got with random BigInts from our discussion in julia-dev.

@andrioni
Copy link
Member

Why did you define a second constructor to BigInt?

@jiahao
Copy link
Member Author

jiahao commented May 11, 2013

As you had pointed out, the seed in BigIntRand had to be unpacked to be allocated correctly. I put in the second constructor in case down the line extracting the seed as a BigInt might become desirable.

@andrioni
Copy link
Member

Also, the methods don't follow the API you wrote here, nor the one on random.jl.

rand{T<:Integer}(randstate::BigIntRand,x::Range1{T<:Integer}) at gmp.jl:354
rand!{T<:Integer}(r::Range1{T<:Integer},A::Array{T<:Integer,N}) at gmp.jl:362

range.jl will also need changes, as BigInt ranges with length larger than typemax(Int) don't work:

julia> range = BigInt(1):BigInt("127138971893189187381")
ERROR: InexactError()
 in convert at gmp.jl:67
 in colon at range.jl:38

You also need to re-seed the functions someway, as rand!{T<:Integer}(r::Range1{T<:Integer},A::Array{T<:Integer,N}) doesn't work properly because it always get seeded with zeros.

@andrioni
Copy link
Member

The problem of using this to extract the seed is that we can't control when the finalizer will be called, and this means it is possible for the seed to be freed while BigIntRand is still alive.

@jiahao jiahao deleted the jh/gmp-rand branch December 29, 2013 23:10
@ViralBShah ViralBShah added the randomness Random number generation and the Random stdlib label Nov 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants