Hey guys, I started my studies with NIM language just a few days ago, so Im learning yet.
I need to generate a random integer number, given a range. I was looking for something and found this at the NIM Manual:
proc random[T](x: Slice[T]): T
proc random[T](a: openArray[T]): T
But I'm not sure how to use it, any tips?Yes, when you have never done any computer programming before it may be not obviously.
Generally nearly every programming language has a function random() which takes an integer argument n and returns a random number from 0 to n-1.
Some languages provides a random() function which takes no argument and returns floating point numbers between 0 and 1. Nim seems to have a modified version.
See
Note that you will get the some sequence of random numbers unless you call randomize() before! And also note that proc name is now rand() for latest Nim version
import random
for i in 0 .. 2:
echo random(6) + 1 # rolling a dice with numbers 1 .. 6
echo random(10.0) # floating point numbers from 0.0 upto 10.0 (excluding)
echo random(["araq", "dom", "zahary"]) # name of Nim developer
echo random(["araq", "dom", "zahary"][0 .. 1]) # araq or dom
random proc is deprecated in favor of rand proc in nim devel version. The semantics is almost the same, except the ranges are inclusive where applicable.
echo rand(6) # 0 .. 6
echo rand(1 .. 6) # 1 .. 6