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

rand(num) won't work with big_ints #5647

Closed
jzakiya opened this issue Jan 27, 2018 · 6 comments
Closed

rand(num) won't work with big_ints #5647

jzakiya opened this issue Jan 27, 2018 · 6 comments

Comments

@jzakiya
Copy link

jzakiya commented Jan 27, 2018

crystal 0.24.1

translating code from Ruby and hit this bug when doing

rand(self-4)  

when self is big_int

@oprypin
Copy link
Member

oprypin commented Jan 28, 2018

Hi,
I'll try to explain the lack of response:
This is a valid feature request but probably very low priority.

In most cases you can work around this by getting a random Int64 or Float64 first

@jzakiya
Copy link
Author

jzakiya commented Jan 30, 2018

The hack below at least got the code to compile, but it's sooooo ugly, and probably doesn't have the statistical properties of a real random distribution.

b = 2 + ((self-4).to_f * Random.rand).to_big_i

@iSarCasm
Copy link
Contributor

iSarCasm commented Sep 8, 2018

Hello. What is the best way to implement random for BigInts @ysbaddaden ? There is some random inside GMP or should I just dance with Random::Secure.next_u?
https://github.com/crystal-lang/crystal/blob/master/src/random.cr
https://github.com/crystal-lang/crystal/blob/master/src/random/secure.cr
Thanks

@oprypin
Copy link
Member

oprypin commented Sep 8, 2018

If covering all possible values is not critical, you can work around this with rand(range : Range(Float, Float)) : Float and rounding.

@oprypin
Copy link
Member

oprypin commented Sep 8, 2018

And the proper solution would be extending the algorithm to support BigInt (shouldn't be too hard)

@iSarCasm
Copy link
Contributor

iSarCasm commented Sep 8, 2018

@oprypin Yes, I need the whole range for the cryptographic algorithm. Thanks, I will try to extend this alg.

oprypin added a commit to oprypin/crystal that referenced this issue Sep 8, 2018
oprypin added a commit to oprypin/crystal that referenced this issue Sep 8, 2018
RX14 pushed a commit that referenced this issue Sep 10, 2018
* Implement Random for BigInt

Fixes #5647

* spec typo fix

* Move to big_int.cr

* Use to_s in spec

* Remove special case of zero because it will be removed in #6686
ezrast pushed a commit to ezrast/crystal that referenced this issue Oct 2, 2018
* Implement Random for BigInt

Fixes crystal-lang#5647

* spec typo fix

* Move to big_int.cr

* Use to_s in spec

* Remove special case of zero because it will be removed in crystal-lang#6686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants