Languages
[Edit]
EN

JavaScript - random short value

7 points
Created by:
Aran-Busby
592

In this short article, we would like to show to randomize short value using JavaScript.

Note: as short value we understand signed 16 bits integer value (from -32768 to 32767).

Quick solution:

const value = Math.floor(65535 * Math.random()) - 32768;

 

Reusable function

// ONLINE-RUNNER:browser;

const randomShort = () => Math.floor(65535 * Math.random()) - 32768;


// Usage example:

console.log(randomShort());  //  -3870
console.log(randomShort());  //  17488
console.log(randomShort());  // -28905

 

See also

  1. JavaScript - random boolean value

  2. JavaScript - random byte value

  3. JavaScript - random int value

  4. JavaScript - random long value

Alternative titles

  1. JavaScript - random Int16 value
  2. JavaScript - random 16 bits signed int value
  3. JavaScript - random <-32768, +32767> value
  4. JavaScript - random sshort value
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join