From cb9e359a51c3249d8f5157db69d43fd413ddeda6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:45:12 +0100 Subject: unslug ca: move --- .../global_objects/math/random/index.html | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 files/ca/web/javascript/reference/global_objects/math/random/index.html (limited to 'files/ca/web/javascript/reference/global_objects/math/random') diff --git a/files/ca/web/javascript/reference/global_objects/math/random/index.html b/files/ca/web/javascript/reference/global_objects/math/random/index.html new file mode 100644 index 0000000000..d70169efd4 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/math/random/index.html @@ -0,0 +1,126 @@ +--- +title: Math.random() +slug: Web/JavaScript/Referencia/Objectes_globals/Math/random +translation_of: Web/JavaScript/Reference/Global_Objects/Math/random +--- +
{{JSRef}}
+ +

La funció Math.random() retorna un nombre decimal de punt flotant pseudo-aleatori que roman dins el rang [0, 1), és a dir, des de 0 (inclòs) fins a 1 (exclòs), que després pot ser escalat al rang dessitjat. La implementació selecciona la llavor inicial per a l'algoritme generador de nombres aleatoris; aquesta llavor no pot ser resetejada o escollida per l'usuari.

+ +
+

Nota: Math.random() no proporciona nombres aleatoris criptogràficament segurs. No l'utilitzeu per a cap tasca relacionada amb la seguretat. Per a aquest ús utilitzeu la API Web Crypto, i més concretament el mètode {{domxref("RandomSource.getRandomValues()", "window.crypto.getRandomValues()")}}.

+
+ +

Sintaxi

+ +
Math.random()
+ +

Exemples

+ +

Utilitzar Math.random()

+ +

Cal destacar que com que els nombres a JavaScript són nombres de punt flotant IEEE 754 amb comportament d'arrodoniment al parell més proper, els rangs proclamats per les funcions de sota (a excepció de Math.random()) no són exactes. Si s'escullen límits extremadament grans (253 o majors), és possible en casos extremadament rars, obtindre el límit superior que normalment és exclòs.

+ +
// Retorna un nombre aleatori entre 0 (inclòs) i 1 (exclòs)
+function getRandom() {
+  return Math.random();
+}
+
+ +
// Retorna un nombre aleatori entre min (inclòs) i max (exclòs)
+function getRandomArbitrary(min, max) {
+  return Math.random() * (max - min) + min;
+}
+
+ +
// Retorna un nombre sencer aleatori entre min (inclòs) i max (exclòs)
+// Utilitzar Math.round() proporciona una distribució no uniforme!
+function getRandomInt(min, max) {
+  return Math.floor(Math.random() * (max - min)) + min;
+}
+
+ +
// Returns a random integer between min (included) and max (included)
+// Using Math.round() will give you a non-uniform distribution!
+function getRandomIntInclusive(min, max) {
+  return Math.floor(Math.random() * (max - min + 1)) + min;
+}
+ +

Especificacions

+ + + + + + + + + + + + + + + + + + + + + + + + +
EspecificacióEstatComentaris
{{SpecName('ES1')}}{{Spec2('ES1')}}Definició inicial. JavaScript 1.0 (Només UNIX) / JavaScript 1.1 (Totes les plataformes).
{{SpecName('ES5.1', '#sec-15.8.2.14', 'Math.random')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-math.random', 'Math.random')}}{{Spec2('ES6')}} 
+ +

Compatibilitat amb navegadors

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
CaracterísticaChromeFirefox (Gecko)Internet ExplorerOperaSafari
Suport bàsic{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
CaracterísticaAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Suport bàsic{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
-- cgit v1.2.3-54-g00ecf