Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

euphoria
communitycreator

What is the rand_range() method in Euphoria?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Euphoria, there are a sizeable amount of methods which help us do random number manipulations and operations. In a previous shot I wrote some time ago, I explained one of these methods, the rand() method. In this shot, we will look at another such method called the rand_range() method.

The rand_range() method

The rand_range() method produces random numbers between an inclusive range. For instance, rand_range(12,23) can produce outputs that may include any integer between 12 and 23, with 12 and 23 inclusive. Unlike the rand() method, the rand_range() method can be used to indicate the range of values we want as the random numbers.

Syntax

rand_range(lowest,highest)

Parameter values

  • lowest: This is the lowest possible random number that may be randomly generated from the given range.
  • highest: This is the highest possible random number that may be randomly generated from the given range.

Return value

This method returns an atom value, which is any number that is generated randomly from a given range.

Example

The code snippet below shows a simple example of a random number that is generated from a given range.

--import the rand.e module
include std/rand.e
--declare variables
atom output1, output2
--call the rand_range method and save output.
output1 = rand_range(23,40)
--common for OTPs
output2 = rand_range(100000,999999)
print(1,output1)
puts(1,"\n")
print(1,output2)
Using the rand_range method

Explanation

Anytime we hit the “Run” button, we will get a new random value that will fall within the indicated range, inclusive of the lowest and highest range values.

  • Line 2: We include the rand.e module from the standard library. This module allows us to use the inbuilt random methods.

  • Line 5: We Declare some variables, output1, and output2, to hold the result after using the rand_range() method.

  • Lines 8 and 11: We call the rand_range() method.

  • Lines 13–15: We print some values to the console.

RELATED TAGS

euphoria
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring