Feature request: Array.prototype.random

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Feature request: Array.prototype.random

Will White
Dear es-discuss,

I would like to propose Array.prototype.random, which would return a random array element the same way `array[Math.floor(Math.random() * array.length)]` does, because every time I want a random array element, I have to remember how to do it. Do you think this is a useful addition to the language?

Will White
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

Michał Wadas
I believe it's too specialized to be a part of Array interface. 

Though, I think JS should have better support for randomness, so we could do:

prng.pick(arr);
prng.sample(arr, 5);
prng.sampleWithoutRepetitions(arr, 4);

On 15 Jun 2017 20:42, "Will White" <[hidden email]> wrote:
Dear es-discuss,

I would like to propose Array.prototype.random, which would return a random array element the same way `array[Math.floor(Math.random() * array.length)]` does, because every time I want a random array element, I have to remember how to do it. Do you think this is a useful addition to the language?

Will White
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

Will White
And prng.pick(str), prng.pick(set) etc.?

On 15 Jun 2017, at 22:34, Michał Wadas <[hidden email]> wrote:

I believe it's too specialized to be a part of Array interface. 

Though, I think JS should have better support for randomness, so we could do:

prng.pick(arr);
prng.sample(arr, 5);
prng.sampleWithoutRepetitions(arr, 4);

On 15 Jun 2017 20:42, "Will White" <[hidden email]> wrote:
Dear es-discuss,

I would like to propose Array.prototype.random, which would return a random array element the same way `array[Math.floor(Math.random() * array.length)]` does, because every time I want a random array element, I have to remember how to do it. Do you think this is a useful addition to the language?

Will White
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

Isiah Meadows-2
Better idea: let's introduce an integer-based `Math.random` equivalent
that can be optionally constrained to a specific range.


```js
// Simple polyfill
Math.randomInt = Math.randomInt || function (start, end) {
    if (arguments.length === 0) {
        start = 0; end = Number.MAX_SAFE_INTEGER
    } else if (arguments.length === 1) {
        end = start; start = 0
    }
    start = Math.max(Math.floor(start), -Number.MAX_SAFE_INTEGER)
    end = Math.min(Math.floor(end), Number.MAX_SAFE_INTEGER)

    return Math.floor(Math.random() * (end - start)) + start
}
```

You could then use it like this:
`array[Math.randomInt(array.length)]`. I feel in this particular case,
a more general solution is much more useful than just a "pick some
random item in an array". (Number guessing games, anyone?)
-----

Isiah Meadows
[hidden email]


On Thu, Jun 15, 2017 at 6:20 PM, William White <[hidden email]> wrote:

> And prng.pick(str), prng.pick(set) etc.?
>
> On 15 Jun 2017, at 22:34, Michał Wadas <[hidden email]> wrote:
>
> I believe it's too specialized to be a part of Array interface.
>
> Though, I think JS should have better support for randomness, so we could
> do:
>
> prng.pick(arr);
> prng.sample(arr, 5);
> prng.sampleWithoutRepetitions(arr, 4);
>
> On 15 Jun 2017 20:42, "Will White" <[hidden email]> wrote:
>
> Dear es-discuss,
>
> I would like to propose Array.prototype.random, which would return a random
> array element the same way `array[Math.floor(Math.random() * array.length)]`
> does, because every time I want a random array element, I have to remember
> how to do it. Do you think this is a useful addition to the language?
>
> Will White
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

peter miller
Isaiah,

> Better idea: let's introduce an integer-based `Math.random` equivalent
> that can be optionally constrained to a specific range.
>

There's a discussion of it here:

https://github.com/rwaldron/proposal-math-extensions/issues/8

Peter

--
"There were drawings, and sheets of paper with writing on them, and it  
seemed that they were the sustenance of life, that here were the warlocks,  
almost the vehicles of destruction of man's life, but at the same time the  
very reason for his living." --- Maeve Gilmore/Titus Awakes.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

Henrik Sommerland
In reply to this post by Isiah Meadows-2
I agree that having a random integer function would be more suitable. 
Picking a random element from an array is a fairly uncommon thing but
generating a random integer in a given range is something much more desirable.

2017-06-20 11:33 GMT+02:00 Isiah Meadows <[hidden email]>:
Better idea: let's introduce an integer-based `Math.random` equivalent
that can be optionally constrained to a specific range.


```js
// Simple polyfill
Math.randomInt = Math.randomInt || function (start, end) {
    if (arguments.length === 0) {
        start = 0; end = Number.MAX_SAFE_INTEGER
    } else if (arguments.length === 1) {
        end = start; start = 0
    }
    start = Math.max(Math.floor(start), -Number.MAX_SAFE_INTEGER)
    end = Math.min(Math.floor(end), Number.MAX_SAFE_INTEGER)

    return Math.floor(Math.random() * (end - start)) + start
}
```

You could then use it like this:
`array[Math.randomInt(array.length)]`. I feel in this particular case,
a more general solution is much more useful than just a "pick some
random item in an array". (Number guessing games, anyone?)
-----

Isiah Meadows
[hidden email]


On Thu, Jun 15, 2017 at 6:20 PM, William White <[hidden email]> wrote:
> And prng.pick(str), prng.pick(set) etc.?
>
> On 15 Jun 2017, at 22:34, Michał Wadas <[hidden email]> wrote:
>
> I believe it's too specialized to be a part of Array interface.
>
> Though, I think JS should have better support for randomness, so we could
> do:
>
> prng.pick(arr);
> prng.sample(arr, 5);
> prng.sampleWithoutRepetitions(arr, 4);
>
> On 15 Jun 2017 20:42, "Will White" <[hidden email]> wrote:
>
> Dear es-discuss,
>
> I would like to propose Array.prototype.random, which would return a random
> array element the same way `array[Math.floor(Math.random() * array.length)]`
> does, because every time I want a random array element, I have to remember
> how to do it. Do you think this is a useful addition to the language?
>
> Will White
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Feature request: Array.prototype.random

Michael J. Ryan
Just putting in my $.02

I'm thinking math-rnd-util as an npm module would be a place to start...  Then propose extending Math.rnd = rnd-util

Rnd.fill(array)
Rnd.pick(array)
Rnd.pick(min:int, max:int)
...

I suggest flushing things out in a usable npm module would be a good start.

--
Michael J. Ryan - [hidden email] - http://tracker1.info

Please excuse grammar errors and typos, as this message was sent from my phone.

On Jun 20, 2017 3:20 AM, "Henrik Sommerland" <[hidden email]> wrote:
I agree that having a random integer function would be more suitable. 
Picking a random element from an array is a fairly uncommon thing but
generating a random integer in a given range is something much more desirable.

2017-06-20 11:33 GMT+02:00 Isiah Meadows <[hidden email]>:
Better idea: let's introduce an integer-based `Math.random` equivalent
that can be optionally constrained to a specific range.


```js
// Simple polyfill
Math.randomInt = Math.randomInt || function (start, end) {
    if (arguments.length === 0) {
        start = 0; end = Number.MAX_SAFE_INTEGER
    } else if (arguments.length === 1) {
        end = start; start = 0
    }
    start = Math.max(Math.floor(start), -Number.MAX_SAFE_INTEGER)
    end = Math.min(Math.floor(end), Number.MAX_SAFE_INTEGER)

    return Math.floor(Math.random() * (end - start)) + start
}
```

You could then use it like this:
`array[Math.randomInt(array.length)]`. I feel in this particular case,
a more general solution is much more useful than just a "pick some
random item in an array". (Number guessing games, anyone?)
-----

Isiah Meadows
[hidden email]


On Thu, Jun 15, 2017 at 6:20 PM, William White <[hidden email]> wrote:
> And prng.pick(str), prng.pick(set) etc.?
>
> On 15 Jun 2017, at 22:34, Michał Wadas <[hidden email]> wrote:
>
> I believe it's too specialized to be a part of Array interface.
>
> Though, I think JS should have better support for randomness, so we could
> do:
>
> prng.pick(arr);
> prng.sample(arr, 5);
> prng.sampleWithoutRepetitions(arr, 4);
>
> On 15 Jun 2017 20:42, "Will White" <[hidden email]> wrote:
>
> Dear es-discuss,
>
> I would like to propose Array.prototype.random, which would return a random
> array element the same way `array[Math.floor(Math.random() * array.length)]`
> does, because every time I want a random array element, I have to remember
> how to do it. Do you think this is a useful addition to the language?
>
> Will White
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss