How to generate a really big random integer in Ruby?

I want to create a 64 bit integer in ruby. I know you have a lot of time in Java, but I'm not sure how you do it in Ruby. Also, how many charecters are in a 64-bit number? Here is an example of what I'm talking about ... 123456789999.

@num = Random.rand(9000) + Random.rand(9000) + Random.rand(9000) 

But I believe that this is very inefficient, and there should be a simpler and more concise way to do this.

Thanks!

+4
source share
1 answer

rand can take a range as an argument:

 pa = rand(2**32..2**64-1) # => 11093913376345012184 puts a.class #=> Bignum 

From the document : Bignum objects store integers outside the range of Fixnum. Bignum objects are created automatically when integer calculations otherwise overflow Fixnum. When a calculation using Bignum objects returns a result that will fit into Fixnum, the result is automatically converted ...

+10
source

Source: https://habr.com/ru/post/1494806/


All Articles