D
D
Dmitry2020-05-13 16:10:14
JavaScript
Dmitry, 2020-05-13 16:10:14

How to write a function to output a random number in the range [0, 255]?

Write a function t7 that returns a random color in the format rgb(x,y,z) (string), where x,y,z are random numbers in the range [0, 255].

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MagicMight, 2020-05-13
@Monachdg

const t7 = () => {
 return `rgb(${Math.random()*256<<0}, ${Math.random()*256<<0}, ${Math.random()*256<<0})`;
}

E
Eugene, 2020-05-13
@Nc_Soft

https://github.com/ckknight/random-js

W
wertuda, 2021-03-05
@wertuda

function t7() {
  let x = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
  let y = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
  let z = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
  return `rgb(${x}, ${y}, ${z})`;
}

I
ipock, 2021-05-29
@ipock

Decision:

function t6(a, b) {
    return Math.floor(Math.random() * (a - b + 1)) + b
}

function t7() {
    return `rgb(${t6(0, 255)}, ${t6(0, 255)}, ${t6(0, 255)})`
}
    document.querySelector('.button').onclick = function () {
    document.querySelector('.out').style.background = t7();
}​

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question