you are viewing a single comment's thread
view the rest of the comments
[–] 0 points 8 months ago (1 child)

@abbadon420@sh.itjust.works @not_IO@lemmy.blahaj.zone @programmer_humor@programming.devI didn't know about this specific syntax you mentioned (import foo.bar as baz; what I'm aware and I use frequently is something like e.g. const log = console.log.bind(console)), I'm not even sure if it works as all my import use cases involve something installed from NPM or a relative-folder module file.But sometimes it's useful, and better, to have parametrized randomness, as in my helper functions I keep reusing across my personal projects:export const rand = (n, x) => Math.random() * (x - n) + n export const irand = (n, x) => Math.round(rand(n, x)) export const choose = a => a[irand(0, a.length-1)](yeah, my choose helper lacks a proper verification of the input parameter, will return undefined if the array is empty or is not an array, but, well, it's for personal projects so I don't really worry about that; also I've been using Ruby more than I use JS, and Ruby got beautifully native array.sample and Random.rand(a..b))

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 8 months ago*

    I think irand should floor instead of round. First, when you want to generate a random number between 0 and 6 it’s often useful to exclude the 6. (See also https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html).

    Second, irand(0, 6) has a higher chance of rolling a 1 than a 0 because the capture area for 1 is [.5, 1.5), while the capture area for 0 is only [0, .5)

    Also if you had chosen any other variable names, it wouldn’t have taken me 5 minutes to figure out what these things do.

    And one more, if you had swapped the input parameters, and defaulted to n=0, you could call the use case “random number up to 6” with just rand(6) instead of rand(0, 6).

    None of that matters in a private project, but… it just itched to point these things out.

  • source
  • parent