AARRGGHHH Posted March 21, 2022 Share Posted March 21, 2022 I need to create a random number between two numbers. For example, between 64 and 255. I then need to convert that number to hex. Assistance is appreciated. Thank you! Link to comment Share on other sites More sharing options...
Ingolme Posted March 21, 2022 Share Posted March 21, 2022 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); 1 Link to comment Share on other sites More sharing options...
AARRGGHHH Posted March 21, 2022 Author Share Posted March 21, 2022 I didn't know toString() could change base. That's VERY informative. As is choosing a random between two numbers. I was going about it entirely the wrong way. Thank you very much! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now