(moved from <#C0B9K7EP2|language-proposals>)
# stdlib
l
(moved from #language-proposals)
@ilya.gorbunov Hi Ilya, the first use case is purely mathematical/experimental, which means you want to pick a random element from a collection for some reason (whatever it might be) - it might be usefult especially in gaming algorithms etc.
The second use case is when I have a list of expensive resources and I want to share them across multiple threads/coroutines. So I currently would like to do something like: val resource = resources.random() synchronized(resource) { //some logic here } BTW if there is a better way or if there is a library for such multi-threaded resource sharing, your suggestions would be greatly appreciated πŸ™‚
s
Would you want it on
Collection
or
List
?
l
Well, I think it would be good if it was available almost for everything πŸ™‚. Like
List
,
Collection
,
Array
,
Set
... maybe even on sequences or iterables (but I am not sure about these ones). I use mostly just the
List
and rarely an
Array
, but it's hard to guess where other people might need this as well.
a
Maybe even Map, will choose a random entry, will return a Pair object with the key value combination
o
Gaming devs are using far more advanced algos than you could put into the stdlib
πŸ‘ 1
l
@orangy Yeah, sorry for misinterpretation πŸ™‚, my previous statement was kind of badly-told. What I meant was just that people in the gaming industry might want to pick random elements more often than others.
a
Well, its easy to do with java.util.Random, perhaps you will implement a random algorithm in pure Kotlin for multiplatform implementation
o
@littlelightcz nope, gaming industry needs far more sophisticated random functions. With some special properties.
l
@orangy ah I see, now I understand πŸ™‚
@Aregev2 + the tricky part could be whether to maintain a single
Random()
instance or not (due to performance)
a
Yeah, didn't think about it
You basically create a new instance in every function call
So how can we create a single instance to all functions call, maybe with an extension property?
l
... or just a static constant πŸ™‚ anyway @orangy has changed my mind a bit about this. Maybe it should be placed in some dedicated library which would bring these extensions of randomness πŸ™‚
a
There could be a kotlinx.random, for multiplatform ramdom generator implementation
πŸ‘ 1
Or a completely unofficial lib
But I don't really know how random works, it's something with shr/shl I think
s
Well, the gates were kind of opened with
MutableList<T>.shuffle()
a
Um then you can take list[0]
s
Most issues are the same with that
a
But shuffle() works only for JVM
s
Not since 1.2
a
Also for js and Native?
a
But I checked it in KN, it gave me a compile error
s
Hmm ok. I guess native does not have full support for the stdlib then
l
If the List is big, then
shuffle()
could be quite expensive to call.
i
@littlelightcz We're considering to introduce
Random
API to the common standard library. There will be no function to select a random element from a list for now, but it will be trivial to implement as an extension with that API. See the KEEP: https://github.com/Kotlin/KEEP/issues/131
πŸ‘ 8
g
Really like proposed API for Random πŸ‘ What about mentioned random element selection? It's of course trivial to implement, but maybe make sense to concider adding this extension to stdlib as part of Random proposal? Or this proposal exclusively about Random class?
k
A bit off topic, but why is there some negativity around creating a new package and why would it need to be imported automatically?
import
statements are next to trival, no one ever reads them.
i
@gildor Makes sense but requires some additional design and discussion to evaluate how it would fit with the existing collection extensions
@karelpeeters Sure, it's not a question when authoring some third party library, but in the standard library it's about consistency with the other core API.
l
@ilya.gorbunov thanks a lot, that sounds great! πŸ™‚
a
Hope it will make it in the future 1.3, 😁😁
And maybe a function that takes a random element between the elements that match a certain predicte?
k
.filter { ... }.choose()
a
Also Ok
t
Somewhat relevant, with all the stochastic optimization work I'm doing I'm putting together several flavors of random functionality in Kotlin-Statistics, including taking random elements from Sequences/Collections. I also have a weighted coin/dice which you'd likely use to discretely random select an enumerable with a bias towards each outcome. https://github.com/thomasnield/kotlin-statistics/issues/26
l
Regarding the second use case (expensive resource sharing), I've already started with some simple sketch of a library that should cover this. If it will work out well, maybe JetBrains team could also consider moving it to the standard coroutines library or implement their own version of a "resource pool" for concurrent usage. Let me know your opinions! πŸ™‚ https://github.com/LittleLightCz/Krool