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

  • Home
  • SEARCH
  • 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 8858397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:49:10+00:00 2026-06-14T14:49:10+00:00

Good morning, I have to create a random number from a given 256HASH using

  • 0

Good morning,

I have to create a random number from a given 256HASH using a secret key. Everyting is fine, until I have to “convert” the hash into an integer between 0 and 15.000.000 (which is the random number). I have been playing with ord() and bytes but I don’t get anything that suits me.

My original idea was to cast the SHA256 string into an integer, and then apply a divisor to obtain the modulus. But I need a random number between a very big range. 0 to 15.000.000 (fifteen million). How would you do it?

Thanks!

  • 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-14T14:49:12+00:00Added an answer on June 14, 2026 at 2:49 pm

    A hash i basically a number, just with a base of 16. Having said that, you just have to convert it to an int.

    The problem is, when you convert a hash into a int like so:

    echo intval(hash('sha256','asdf'),16);
    

    You will always get 2147483647 for 32-bit systems, which is the maximum value for intval.

    My suggestion would be to cut the value of the hash to a few first characters like so

     echo intval(substr(hash('sha256','bsbaf'),0,6),16);
    

    This is now the seed for the random number, you can get the random by:

     $hash = hash('sha256','bsbaf');
     $seed = intval(substr($hash,0,6),16);
     mt_srand($seed);
     echo mt_rand(0,15000000);
    

    Note that in some cases, you can have collisions, because you are using only the first 6 chars of the hash, but for most uses this will not be a problem. It’s up to you to decide if this is acceptable since I do not know your specific use case.

    UPDATE: An alternative is to make modulo from the resulting seed – like so:

     $hash = hash('sha256','bsbaf');
     $seed = intval(substr($hash,0,6),16);
     echo $seed % 15000000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning gang. I have a jQuery Datepicker object that is prepoulated using a
Good morning, I have to use the properties from DeviceNetworkInformation in my application. (http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.devicenetworkinformation(v=vs.92).aspx)
Good morning, I have problem with using MasterPage and jQuery. I using jQuery UI
Good morning!! I have a dynamic datatable and I'm using Columns of Richfaces, but
Good Morning. I have a question about how to create an application without GUI.
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Good morning. I'm on linux, using cocos2d-x for Android . I have created a
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
Good morning, I am unsuccessfully trying to create a report using mysql/php, I would
Good Morning, I have created my first generic method pieced together from a bunch

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.