How to generate random numbers in Euphoria
Random numbers
A random number is a number that is generated without any form of bias, usually from within a specified range.
Random number functions in programming languages are used to generate a number(s), which can make a predetermined decision based on the random number that is generated. In Euphoria, the rand() method is an in-built function that allows us to generate random numbers.
The rand() method generates random numbers whenever it is called. These random numbers cannot be greater than the maximum value that is passed as the function argument.
Syntax
rand(max_value)
Parameters
max_value: Themax_valueparameter is anatomor asequenceof integers that indicate the highest value of the random numbers that are generated.
Return value
The generated random number is returned for use.
Note: To use this function, we have to include the
rand.efile, which is part of Euphoria’s standard library, in our program code like so:
include std/rand.e
Code example
The code written below will generate random numbers for the atom and the sequence in the for loop. Every time we click the “Run” button, a new set of random numbers will be generated.
include std/rand.eatom num1 = 5sequence numSet = {4,5,6}for i = 1 to 4 doprint(1,rand(num1))puts(1,'\n')print(1,rand(numSet))puts(1,'\n')end for
Code explanation
- Line 2: We import the
rand.efile from the standard library in Euphoria. - Lines 4–5: We assign values to
num1andnumSet, which will serve as themax_valueparameter of therandfunction. - Lines 7–12: These lines contains the start and end of a
forloop, which will print the randomly generated numbers for three iterations, as is indicated in the loop condition. - Line 8–10: Inside the print functions, the
rand()method is called with themax_valuethat was declared earlier as the parameter.