洗牌

使用随机函数

  • "math/rand"
package main

import (
    "math/rand"
    "time"
    "fmt"
)

func main() {
    v1 := shuffle_48()
    fmt.Println(v1)
}
//定义长度
const CardListLen48 = 48

//除去大怪,小怪,红桃2,*花2,方片2,黑桃A
var cardListAll = []int32{
    0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,                    // ♥️
    14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,          // ♠️
    26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,        //♣️
    39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,          //   ♦️
}

//权重
func shuffle_48() []int32 {
    cl := make([]int32,CardListLen48)
    cl = cardListAll
    for i := range cl {
        s := rand.New(rand.NewSource(time.Now().UnixNano()))
        j := s.Intn(CardListLen48) //0 ~ 随机最长数值,还有直接返回int32 ...
        cl[i], cl[j] = cl[j], cl[i] //交换位置
    }
    return cl
}

你可能感兴趣的:(洗牌)