首页 > 其他分享 >Golang基础-随机数

Golang基础-随机数

时间:2023-02-19 16:22:27浏览次数:28  
标签:rand Intn int random 基础 Golang 随机数

import "math/rand"
n := rand.Intn(100) // n is a random int, 0 <= n < 100
f := rand.Float64() // f is a random float64, 0.0 <= f < 1.0

x := []string{"a", "b", "c", "d", "e"}
// shuffling the slice put its elements into a random order
rand.Shuffle(len(x), func(i, j int) {
	x[i], x[j] = x[j], x[i]
})

// 设置随机种子
rand.Seed(time.Now().UnixNano())

标签:rand,Intn,int,random,基础,Golang,随机数
From: https://www.cnblogs.com/roadwide/p/17134943.html

相关文章

  • 机器学习-numpy基础用法
    NUMPY基础#基础语法#输出[1258],类型为<class'numpy.ndarray'>,从python列表构建numpy数组a=np.array([1,2,5,8])#制造一系列等距点,组成数组,前者为浮点数,......
  • Golang基础-Structs与Methods
    将struct定义为一种类型CarNewCar函数return&Car{},返回指针//car.gopackageelon//Carimplementsaremotecontrolledcar.typeCarstruct{ speed......
  • golang 数组
    1.概念golang中的数组是具有固定长度及相同数据类型的序列集合2.初始化数组var数组名[数组大小]数据类型packagemainimport"fmt"funcmain(){ //第一种 v......
  • Golang字符串拼接
    使用+funcplusConcat(nint,strstring)string{ s:="" fori:=0;i<n;i++{ s+=str } returns}使用fmt.SprintffuncsprintfConcat(nint,str......
  • Golang基础-Switch
    不需要手动breakdefault是找不到case时执行可以对多个case执行同样的操作operatingSystem:="windows"switchoperatingSystem{case"windows","linux"://......
  • golang for 循环
    1.for循环for循环是Golang唯一的循环语句。for初始表达式;布尔表达式;迭代因子{循环体;}packagemainimport"fmt"funcmain(){ fori:=0;i<5;i++{......
  • TS基础知识点
    前言:TS简介相关介绍就不一一赘述,网上自行按照需求搜索查阅即可1.TypeScript的静态类型TypeScript的一个最主要特点就是可以定义静态类型,英文是StaticTyping。那到底......
  • golang 单测运行单个函数、文件、跳过文件命令
    1、单测运行1.2运行某个单测函数gotest-v-run=xxx,xxx是函数名,支持正则表达式;参数-v说明需要打印详情提示Golang单测是根据前缀匹配来执行的,gotest-v-run=......
  • K8S的基础概念
    一、Kubernetes介绍1、什么是Kubernetes?Kubernetes(通常称为K8s,K8s是将8个字母“ubernete”替换为“8”的缩写)是一个以容器为中心的基础架构,可以实现在物理集群或虚拟......
  • Linux基础 - 进程管理 ps
     psaux|sort-k4,4nr|head-n10 #查看内存占用前10名的程序ps-e/ps-ef/ps-eF/ps-ely  #Toseeeveryprocessonthesystemusingstandardsy......