Readable

Circular random() for rarest traits

Imagine you want to make a generative project. You want the work to have one of 8 traits and you achieve it like this:

let options=['馃崚','馃崜','馃崌','馃崕','馃崑','馃崓','馃崗','馃ゥ',]
let optionsIndex = Math.floor(Math.random() * options.length)
let trait = options[optionsIndex]

Then you run your script many times and print statistics.

let options = ["馃崚", "馃崜", "馃崌", "馃崕", "馃崑", "馃崓", "馃崗", "馃ゥ"];
let stats = Array(8).fill(0)
for (let i = 0; i < 100; i++) {
  let optionsIndex = Math.floor(Math.random() * options.length);
  let trait = options[optionsIndex];
  stats[optionsIndex] ++
}
for(let i=0;i<8;i++){
  console.log(options[i].repeat(stats[i]))
}

Yes, you are right, line #5 is useless

That's what you get:

馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚 
馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜 
馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌 
馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕馃崕 
馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑 
馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓 
馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗馃崗 
馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ 

Apples got lucky this time

Then you decide you need coconuts to be more rare than cherries. No, you want coconuts to be the most rare trait in your project! One in hundreds! Therefore you change your random function to squared:

let optionsIndex = Math.floor(Math.random()**2 * options.length);

And you get a distribution similar to this:

馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚 
馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜 
馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌 
馃崕馃崕馃崕馃崕馃崕馃崕馃崕 
馃崑馃崑馃崑馃崑馃崑馃崑馃崑馃崑 
馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓馃崓 
馃崗馃崗馃崗馃崗馃崗馃崗 
馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ馃ゥ

No way, apples

Cherries are the most common now! Isn't it great? Wait, coconut was meant to be the rarest, but they are as rare as the bottom half of the list!

Let's see why it happens:

You see, the cherry area on the horizontal axis is huge, but coconut area is as wide as its neighbours.

Blame on parabola! We need a circle here:

let optionsIndex = Math.floor((1-Math.sqrt(1-Math.random()**2)) * options.length);

Here's how it is different:

Pay attention, how tiny the coconut stripe is! Let's run the test again:

馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚馃崚 
馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜馃崜 
馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌馃崌 
馃崕馃崕馃崕馃崕馃崕馃崕馃崕 
馃崑馃崑馃崑馃崑 
馃崓馃崓馃崓馃崓 
馃崗馃崗 
馃ゥ

Circular random test results

We were lucky to get even one, there are no coconuts most of the time.

We did it, hooray. That's a wrap.