Related Tags

euphoria
random numbers
communitycreator

# How to generate random numbers in Euphoria

NDUKWE CHIDERA K.

### 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: The max_value parameter is an atom or a sequence of 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.e file, 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.e

atom num1 = 5
sequence numSet = {4,5,6}

for i = 1 to 4 do
print(1,rand(num1))
puts(1,'\n')
print(1,rand(numSet))
puts(1,'\n')
end for

### Code explanation

• Line 2: We import the rand.e file from the standard library in Euphoria.
• Lines 4–5: We assign values to num1 and numSet, which will serve as the max_value parameter of the rand function.
• Lines 7–12: These lines contains the start and end of a for loop, 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 the max_value that was declared earlier as the parameter.

RELATED TAGS

euphoria
random numbers
communitycreator

CONTRIBUTOR

NDUKWE CHIDERA K.
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time