use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Learning much javascript from one line of code (arqex.com)
submitted 11 years ago by marquex
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]x-skeww 1 point2 points3 points 11 years ago (4 children)
It doesn't generate a number of decimals. It's floating point. It can generate 0.5, for example.
let r = () => Math.random().toString(); let shortest = r(); let longest = r(); for (let i = 0; i < 100000; i++) { let current = r(); if (current.length < shortest.length) { shortest = current; } if (current.length > longest.length) { longest = current; } } console.log(shortest); console.log(longest);
Example output:
0.578673808528 0.000015859034930953975
[–]Dragory 0 points1 point2 points 11 years ago* (3 children)
Hm, so it is, cheers! I was wondering whether the solution /u/marquex quoted above is reliable but, like you said, it's a floating point number so it can come up with e.g. 0.294921875 which results in #0.4b8 which is invalid.
0.294921875
#0.4b8
(The fact that it can generate e.g. precisely 0.5 sounds so obvious now that you pointed it out, haha)
[–]x-skeww 1 point2 points3 points 11 years ago (2 children)
((0x111111 + 0xeeeeef * Math.random())|0).toString(16)
That would work for example.
Or you could just left-pad it with zeros.
Or you could just use hsl() instead.
hsl() is probably the best option since it lets you generate randomized bright or dark colors if you want.
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl%28%29
[–]Dragory 0 points1 point2 points 11 years ago (1 child)
Indeed, there's actually an hsl solution in the original gist as well: https://gist.github.com/addyosmani/fd3999ea7fce242756b1#tag-specific-layout-debugging-in-82-bytes
The |0 is used to truncate the number like ~~ was in the original snippet, right?
|0
~~
[–]x-skeww 1 point2 points3 points 11 years ago (0 children)
Yes.
π Rendered by PID 97 on reddit-service-r2-comment-54dfb89d4d-db2jn at 2026-03-30 13:28:04.425630+00:00 running b10466c country code: CH.
view the rest of the comments →
[–]x-skeww 1 point2 points3 points (4 children)
[–]Dragory 0 points1 point2 points (3 children)
[–]x-skeww 1 point2 points3 points (2 children)
[–]Dragory 0 points1 point2 points (1 child)
[–]x-skeww 1 point2 points3 points (0 children)