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

19th Jul 2018, 2:22 AM
Daniel Cooper
Daniel Cooper - avatar
9 Respostas
+ 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
19th Jul 2018, 2:35 AM
Duncan
Duncan - avatar
+ 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!
19th Jul 2018, 2:49 AM
Paul Grasser
Paul Grasser - avatar
+ 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
19th Jul 2018, 5:28 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Daniel Cooper , check my code again. I think that's more than enough to help you with what you want to do.
19th Jul 2018, 3:13 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 2
Jonathan Pizarra nice one, I'll have to explore the toString() function.
19th Jul 2018, 4:02 AM
Duncan
Duncan - avatar
+ 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
19th Jul 2018, 2:51 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
0
Can you guys give me examples? Use the code I gave you. It would help me understand it better
19th Jul 2018, 2:59 AM
Daniel Cooper
Daniel Cooper - avatar
0
This is the code I came up with. Is this good? https://code.sololearn.com/Wzq0B1J6w95q/?ref=app
19th Jul 2018, 3:13 AM
Daniel Cooper
Daniel Cooper - avatar
19th Jul 2018, 3:14 AM
Paul Grasser
Paul Grasser - avatar