Jump to content

Creating A Random Number Between Two Numbers and Convert to Hex


AARRGGHHH

Recommended Posts

The toString() method of Javascript numbers lets you choose a base. If you choose base 16 it will be shown as hexadecimal.

Here's how to generate a random number and show it in hexadecimal:

// Find a random number between min and max (inclusive)
let min = 64;
let max = 255;
let range = 1 + max - min;
let number = Math.floor(Math.random() * range + min);

// Display the number as hex
let hex = number.toString(16);
console.log(hex);

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...