Rietta
Rietta: Web Apps Where Security Matters
You are reading The Rietta Blog, a publication about the web since 2005.

[Rails] Good Random Positive Integer

Tonight I needed a quick way to generate a good pseudo random number. The following statement in Ruby will generate a positive integer between 0 and the maximum integer supported on the system.

1
  SecureRandom.random_number((2**(0.size * 8-2)-1))

Running that 10 times as a test on my system returned:

  • 479960941838047707
  • 4598189742420362323
  • 4319555246297899788
  • 79907720343840910
  • 1019099276589074756
  • 1753578159791031009
  • 51485412595337811
  • 3333462064391733874
  • 2622897372121370782
  • 4287564549349999056

That’s all for tonight.

Sources

About Frank Rietta

Frank Rietta's photo

Frank Rietta is specialized in working with startups, new Internet businesses, and in developing with the Ruby on Rails platform to build scalable businesses. He is a computer scientist with a Masters in Information Security from the College of Computing at the Georgia Institute of Technology. He teaches about security topics and is a contributor to the security chapter of the 7th edition of the "Fundamentals of Database Systems" textbook published by Addison-Wesley.