Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8573351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:13:45+00:00 2026-06-11T19:13:45+00:00

Trying to generate random digits with SecureRandom class of rails. Can we create a

  • 0

Trying to generate random digits with SecureRandom class of rails. Can we create a random number with SecureRandom.hex which includes only digits and no alphabets.

For example:

Instead of

SecureRandom.hex(4)
=> "95bf7267"

It should give

SecureRandom.hex(4)
=> "95237267"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:13:46+00:00Added an answer on June 11, 2026 at 7:13 pm

    Check out the api for SecureRandom: http://rails.rubyonrails.org/classes/ActiveSupport/SecureRandom.html

    I believe you’re looking for a different method: #random_number.

    SecureRandom.random_number(a_big_number)
    

    Since #hex returns a hexadecimal number, it would be unusual to ask for a random result that contained only numerical characters.

    For basic use cases, it’s simple enough to use #rand.

    rand(9999)
    

    Edited:

    I’m not aware of a library that generates a random number of specified length, but it seems simple enough to write one. Here’s my pass at it:

    def rand_by_length(length)
      rand((9.to_s * length).to_i).to_s.center(length, rand(9).to_s).to_i
    end
    

    The method #rand_by_length takes an integer specifying length as a param and tries to generate a random number of max digits based on the length. String#center is used to pad the missing numbers with random number characters. Worst case calls #rand for each digit of specified length. That may serve your need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate a random number that only populates a certain percent
I am trying with below code to generate 10 digits unique random number. As
I'm trying to generate 16 000 000 unique random numbers (10-digits: range 1 000
What is the best way to generate a random DateTime in Ruby/Rails? Trying to
I have a number of rectangles, and am trying to generate a random point
i am trying to generate random number for my mental math quiz game. But
I am trying to generate a random prime number of type BigInteger, that is
I am trying to generate a random number, but it should not be a
I am trying to generate a random number between two floats (the max is
I am trying to generate a large data set which includes time datatype in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.