30% Chance Math.random()
// 30%if (Math.random() < .3) {
// 30% chance this will runconsole.log('30%');
} else {
console.log('nope');
}
I do this all the time when creative coding. I usually use a seeded random number generator instead of Math.random. It’s very useful to have something happen X% of the time.
Let’s run this a few times and look at the results. Click/tap try it out.
for (let i = 0; i < 111; i++) {
if (Math.random() < .3) {
console.log('30%');
} else {
console.log('nope');
}}
Scroll through the results of the console window.