0
Random hex generator?
So I want this code to generate and display random hex codes. I have no idea where to start. Can anyone help me? https://code.sololearn.com/Wzq0B1J6w95q/?ref=app
9 Antworten
+ 3
This will help with random numbers, but if you're using Hex, you'll need random characters from A to F. It can be done, but is more complicated. Can you just use RGB colours instead, then you just need a random number between 0 and 255.
https://www.w3schools.com/js/js_random.asp
+ 3
Generate random numbers between 0 and 255. Do:
var num = Math.floor(Math.random() *256)
Then, get the Hex equivalent of "num"
num = num.toString(16)
Now num will be a hex. Do it for as many as you want!
+ 3
An alternative using a source string as an array with a random index. The one you have now (random over all 0x1000000 values) is one I also use, but this is useful in certain cases:
var s="#";
while(s.length<7)
s += '01234567890abcdef'[parseInt(Math.random()*16)];
You can also use map, but it's ES6 and above I think
+ 2
Daniel Cooper , check my code again. I think that's more than enough to help you with what you want to do.
+ 2
Jonathan Pizarra nice one, I'll have to explore the toString() function.
+ 1
Daniel Cooper , as Duncan said, you can just use RGB. But since you also want to display the hex value, you can use the toString() method.
This code snippet will give you an idea on how to do it. It's not that complicated.
https://code.sololearn.com/W17q5736iVgA/?ref=app
0
Can you guys give me examples? Use the code I gave you. It would help me understand it better
0
This is the code I came up with. Is this good?
https://code.sololearn.com/Wzq0B1J6w95q/?ref=app
0
Here, have a look at this:
https://code.sololearn.com/W0rjdnf1J6si/?ref=app