mardi 14 février 2023

How could I pick a random item from a slice with bias towards one end of the slice using Go?

I understand one way to pick a random value from a slice in Go:

rand.Seed(time.Now().UTC().UnixNano())     
                                                
var db [500]string                         
log.Println(db[rand.Intn(len(db))])        

But how would I pick a random item from the slice, with a bias towards one end of the slice? For my use case I'll have a slice that grows over time using append(). My understanding is that the newest items will be added to the right-hand side of the slice. I'd like to create a function that picks a random item from the slice, with bias towards the newest members of the slice. My first guess is to use a normal distribution via rand.NormFloat64(), but I'm not sure how or if I can use it to achieve this.

The function should be capable of picking any item from the slice, but should pick new items added to the slice at a much higher frequency.




Aucun commentaire:

Enregistrer un commentaire