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 8934513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:48:18+00:00 2026-06-15T09:48:18+00:00

It looks like Math.random() generates a 64-bit floating point number in the range [0,1)

  • 0

It looks like Math.random() generates a 64-bit floating point number in the range [0,1) while the new crypto.getRandomValues() API only returns ints. What would be the ideal way to produce a number in [0,1) using this API?

This seems to work but seems suboptimal:

ints = new Uint32Array(2)
window.crypto.getRandomValues(ints)
return ints[0] / 0xffffffff * ints[1] / 0xffffffff

EDIT: To clarify, I am trying to produce better results than Math.random(). From my understanding of floating point, it should be possible to get a fully random fraction for 52 bits of randomness. (?)

EDIT 2: To give a little more background, I’m not trying to do anything cryptographically secure but there are a lot of anecdotal stories about Math.random() being implemented poorly (e.g. http://devoluk.com/google-chrome-math-random-issue.html) so where a better alternative is available I’d like to use it.

  • 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-15T09:48:19+00:00Added an answer on June 15, 2026 at 9:48 am

    Remember that floating point numbers are just a mantissa coefficient, multiplied by 2 raised to an exponent:

    floating_point_value = mantissa * (2 ^ exponent)
    

    With Math.random, you generate floating points that have a 32-bit random mantissa and always have an exponent of -32, so that the decimal place is bit shift to the left 32 places, so the mantissa never has any part to the left of the decimal place.

    mantissa =         10011000111100111111101000110001 (some random 32-bit int)
    mantissa * 2^-32 = 0.10011000111100111111101000110001
    

    Try running Math.random().toString(2) a few times to verify that this is the case.

    Solution: you can just generate a random 32-bit mantissa and multiply it by Math.pow(2,-32):

    var arr = new Uint32Array(1);
    crypto.getRandomValues(arr);
    var result = arr[0] * Math.pow(2,-32);
    // or just   arr[0] * (0xffffffff + 1);
    

    Note that floating points do not have an even distribution (the possible values become sparser the larger the numbers become, due to a lack of precision in the mantissa), making them ill-suited for cryptographic applications or other domains which require very strong random numbers. For that, you should use the raw integer values provided to you by crypto.getRandomValues().

    EDIT:

    The mantissa in JavaScript is 52 bits, so you could get 52 bits of randomness:

    var arr = new Uint32Array(2);
    crypto.getRandomValues(arr);
    
    // keep all 32 bits of the the first, top 20 of the second for 52 random bits
    var mantissa = (arr[0] * Math.pow(2,20)) + (arr[1] >>> 12)
    
    // shift all 52 bits to the right of the decimal point
    var result = mantissa * Math.pow(2,-52);
    

    So, all in all, no, this isn’t ant shorter than your own solution, but I think it’s the best you can hope to do. You must generate 52 random bits, which needs to be built from 32-bit blocks, and then it need to be shifted back down to below 1.

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

Sidebar

Related Questions

I have a function that looks like so: window.attack = function(enemyHealth){ attackChance = Math.floor(Math.random()*11);
I need to port some JS code which involves Math.random()*2147483648)^(new Date).getTime() . While it
Looks like while( condition ) { //do stuff } is completely equivalent to for(
Looks like operator new and operator new[] have exactly the same signature: void* operator
I got this function in JS var number = Math.random() + ; var r
Looks like this question has been asked thousand times already, but each person's configuration
Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors,
Looks like there is no Load event for usercontrol on the CF. I'm used
Looks like XSD.exe is not delivered as a part of Visual Studio 2010. what
Looks like dynamic memory allocation without garbage collection is a way to disaster. Dangling

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.