vlang

How to generate a random number in vlang?


I want to randomly pick an item in a array.Like javascript code

words[Math.floor(Math.random() * words.length)]

But I don't know how to generate a number like javascript Math.random() function in vlang. Does anyone know ?


Solution

  • Per the documentation at rand, you can use the rand module and for example, the rand.u32n(words.length) function. Make sure you handle the optional case..