4

Is it possible to obtain a Big O estimate of Math.random()?

Corey Farwell
  • 1,856
  • 3
  • 14
  • 19

3 Answers3

8

I'm not sure this question makes much sense. There's no variable size input to increase gauge complexity against - you make a call (with no arguments) and you get an output.

Are you asking if the Math.random() method takes longer for successive calls? Or if it's just slower than it should be?

Remember that even algorithms with O(1) complexity can take a long time - it's just that the length of time they take doesn't depend on how much data is involved.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I don't understand why experts here answer the question in a different way even though they understand the question properly – Sai Nikhil Dec 27 '19 at 02:00
5

The algorithm is documented, see here. It's a Linear congruential generator which ends up having complexity of O(1)

Dr G
  • 3,987
  • 2
  • 19
  • 25
0

it definitely is.. just look at how the function is implemented in the library and then apply recurrence-equations and do some math. ;)

fakemustache
  • 836
  • 6
  • 7
  • I guess you'll get downvoted if you don't elaborate a little bit. – Dr. belisarius Dec 28 '10 at 19:15
  • @belisarius: The OP asked if it was possible, not what the answer was! – President James K. Polk Dec 28 '10 at 19:43
  • @GregS Based on that criteria a simple "Yes." is a good answer. Mmmm ... I guess that is not what SO is about. – Dr. belisarius Dec 28 '10 at 19:55
  • @belisarius: It is the best answer for the amount of information provided, which is not enough to even reliably guess what the OP has in mind. – President James K. Polk Dec 28 '10 at 19:59
  • i hit enter too soon ;) my comment was: well there are several implementations for pseudorandom number generator-algorithms and each can have a different complexity class. (eg. (O(f)=n^3 makes for cubical time-complexity other classes re nlogn, logn, n, n^2, n^k and so on). so if you have the code to a function you can calculate the worst-case complexity of it. there is a lot of material out there to read about this topic, its a bit hard to explain in 3 sentences. i hope i could help a little.. ;) – fakemustache Dec 30 '10 at 02:48