[SOLVED] Math.random explanation error

[0,1) instead of (0,1)
image

Same typo here:

random(min, max) link

Return a pseudo-random number between min and max. The number generated is in the range [min, max), that is inclusive of the minimum but exclusive of the maximum.

And if I give this explanation I can define min and max myself, but that is not the case?

Where is the error? If you mean the bracket, then that is a correct mathematical notion for “inclusive” vs “exclusive”.

2 Likes

I didn’t know that! Have to remember that one.

1 Like

So for example:

Math.random(10, 200);
1 Like

Hmm, not sure what you mean @Albertos. The random number is chosen from the interval range, specified by the endpoints that you provide as arguments. An endpoint can be either exclusive or inclusive, creating an open (10, 200), half-closed (10, 200] | [10, 200) or closed interval [10, 200].

PlayCanvas has a method .random(), which creates a half-closed interval, that contains one endpoint, but not the other. To be more specific, it includes the min, but not the max. It does not offer methods to create other intervals, but you can write your own ones, as they are trivial.

Hay @LeXXik. What I mean is that as far as I know the number is always somewhere between 0 and 1. So why does the explanation speak about min and max and not about 0 and 1 (or 0 and 0.9)?

1 Like

Oh, I see. No, this is a PlayCanvas method, and as such it is different from the Javascript standard one that you are used to. The PC one has a different implementation for convenience and allows something like pc.math.random(10, 200); // 43.1234567

1 Like

So if I understand correctly Math.random() gives a number between 0 and 1. And with pc.math.random() can I choose my own minimum and maximum? Stands pc for PlayCanvas?

Yes @Albertos exactly, it’s quite a useful method.

pc.math in general holds a number of useful math related methods.

https://developer.playcanvas.com/en/api/pc.math.html

Ah okay. Learned something again. Thanks @LeXXik and @Leonidas!

2 Likes

Oh, thanks, I did not know that