funkia / list

🐆 An immutable list with unmatched performance and a comprehensive functional API.
MIT License
1.65k stars 53 forks source link

.random possible? #81

Closed NightScript370 closed 4 years ago

NightScript370 commented 4 years ago

How would I go about making a .random function that would allow me to get a random value of the list? I can do it for normal arrays but I'm not sure how to do it for list.

Normal array Random code:

Array.prototype.random = function() {
            return this[Math.floor(Math.random() * this.length)];
        };
paldepind commented 4 years ago

Hi @NightYoshi370.

Why did you close the issue?

This should work:

function random(list) {
  return list.nth(Math.floor(Math.random() * list.length));
};

The code list.nth(n) is equivalent to array[n].

I hope that helps :smile: