我想在array.Like javascript代码中随机选择一项
words[Math.floor(Math.random() * words.length)]但我不知道如何在vlang中生成像javascript Math.random()函数这样的数字。有人知道吗?
发布于 2022-04-19 08:49:56
根据兰德的文档,您可以使用rand模块,例如rand.u32n(words.length)函数。确保你处理可选的案子..。
发布于 2022-05-26 13:55:38
有几种方法
chooseimport rand
words := ['one', 'two', 'three']
word := rand.choose<string>(words, 1) or {[words[0]]} // this is a list
println(word[0])intnimport rand
words := ['one', 'two', 'three']
word := words[rand.intn(words.len) or {0}]
println(word)https://stackoverflow.com/questions/71921770
复制相似问题