-3

I need to create a long random number with length min 0 and max 90000000000000000

ex: 23746589201948757493028

it is possibile?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
user3477026
  • 241
  • 1
  • 3
  • 15
  • 1
    Yes and no. You can create any number you want, but you will loose precision. The maximum "safe" integer value is `9007199254740991`. – Felix Kling Feb 18 '15 at 20:13
  • 1
    Well if you don't actually need to *do* anything with the number, you can just generate a string of random digit characters. What are you trying to achieve? – Pointy Feb 18 '15 at 20:14
  • Just combine two functions with the maximum number to get it – user3477026 Feb 18 '15 at 20:22
  • *"Just combine two functions with the maximum number to get it"* I have the impression you are missing basic understanding of how numbers are represented in a program, but feel free to answer your own question with a working solution if you have one. – Felix Kling Feb 18 '15 at 20:27
  • function getRandomInt(min, max) { return Math.floor(Math.random() * (min - max + 1)) + min; } alert(getRandomInt(0,999999999999) + getRandomInt(0,999999999999)); NIUB! – user3477026 Feb 18 '15 at 20:29
  • Uhm, `2 * 999999999999` is well in the range of `2^53`. So of course that works. You could also just use `getRandomInt(0, 1999999999998)` in that case (`2 * 999999999999 = 1999999999998`). However, the value you used in your example (`23746589201948757493028`) is much larger than what JS can represent (precisely). The maximum number of digits an integer can have is `16`. – Felix Kling Feb 18 '15 at 20:47

1 Answers1

1

You can't. They are 64-bit floating point values, the largest exact integral value is 253

or

9007199254740992

That said this is the max exact value if you don't care about floating point inaccuracies, which you probably should, you can use higher numbers.

See the spec for more details : http://ecma262-5.com/ELS5_HTML.htm#Section_8.5

ShaneQful
  • 2,140
  • 1
  • 16
  • 22
  • Just combine two functions with the maximum number to get it – user3477026 Feb 18 '15 at 20:20
  • The issue with that is that you'll end up with floating point in accuracies. See here for more details: http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – ShaneQful Feb 18 '15 at 20:22