weight_random_choose

package module
v0.0.0-...-b880a62 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 4 Imported by: 0

README

带权重的随机选择算法(weight random choose)

一、这是什么?使用场景是什么?

通常情况下当我们需要从一个数组中随机选择一个元素的时候,我们一般会在[0, len(array)]区间生成一个随机数,然后使用这个随机数作为数组下标从数组中取一个元素,在这种情况对于数组中的每个元素被选中的概率都是1/n,但是如果我们需要数组中的每个元素被选中的概率都不同呢,比如有一个数组:

["写代码", "看书", "打游戏"]

我们期望数组中的每个元素被选中的概率都不同,比如对于“写代码”的权重高一些,对于其它事情的权重低一些,权重数组大概是这样:

[2, 1, 1]

这个时候对于每件事情随机选中的概率期望为:

写代码 --> 50%
看书 --> 25%
打游戏 --> 25%

这个仓库的目标就是如何实现这种带权重的随机分布。

二、安装

go get -u github.com/cryptography-research-lab/weight-random-choose 

三、API示例

package main

import (
	"fmt"
	weight_random_choose "github.com/cryptography-research-lab/weight-random-choose"
)

func main() {

	// slice是要被选择的数组
	slice := []rune{'A', 'B', 'C'}
	// weights是与slice一一对应的每个元素被选中的的权重
	weights := []int{60, 30, 10}

	// 当需要运行多次带权重的随机选择,创建一个struct性能会比较好
	choose := weight_random_choose.New(slice, weights)

	// 运行足够大的次数,统计每个字符真正被选中的次数
	total := 100000000
	countMap := make(map[rune]int)
	for i := 0; i < total; i++ {
		n := choose.Random()
		countMap[n] += 1
	}

	// 看一下这些字符真正被选中的次数是否符合刚开始的期望
	for _, char := range slice {
		count := countMap[char]
		fmt.Println(fmt.Sprintf("%s %d %f", string(char), count, float64(count)/float64(total)*float64(100)))
	}
	// Output:
	// A 60994473 60.994473
	// B 30007986 30.007986
	// C 8997541 8.997541

}

四、算法详解

TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool)

BinarySearch searches for target in a sorted Slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the Slice. The Slice must be sorted in increasing order.

func RandomChoose

func RandomChoose[T any](slice []T, weights []int) (T, error)

RandomChoose 根据权重数组随机选择一个元素

func RandomChooseFromTupleSlice

func RandomChooseFromTupleSlice[T any](tupleSlice []tuple.Tuple2[T, int]) (T, error)

RandomChooseFromTupleSlice 从随机数组中选择一个元素

Types

type WeightRandomChoose

type WeightRandomChoose[T any] struct {

	// 被带权重随机选择的目标数组
	Slice []T

	// 权重数组
	Weights []int
	// contains filtered or unexported fields
}

WeightRandomChoose 带权重的随机选择,创建一个struct示例多次Random性能稍微好一些

func New

func New[T any](slice []T, weights []int) (*WeightRandomChoose[T], error)

func NewUseTupleSlice

func NewUseTupleSlice[T any](tupleSlice []tuple.Tuple2[T, int]) (*WeightRandomChoose[T], error)

NewUseTupleSlice 从元组数组中创建

func (*WeightRandomChoose[T]) Random

func (x *WeightRandomChoose[T]) Random() T

Random 带权重随机选择一个值

func (*WeightRandomChoose[T]) UpdateWeights

func (x *WeightRandomChoose[T]) UpdateWeights(weights []int) error

UpdateWeights 更新权重数组,当权重有更改的时候不要直接修改Weights数组,而是通过这个方法更新权重

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL