Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

euphoria
communitycreator

What is the roll() 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

The rand.e module of the Euphoria standard library has a lot of functions that can help us when coding instances of uncertainty and randomity. Methods built into this module have operations that range from generating random numbers within a range, calculating the chances of an event, and setting seed for random number generation. In this shot, we will look at the roll() method.

The roll() method

The roll() method simulates the probable outcome of a dice throw. We can use this method to simulate the whole dice throw process when creating a simple web game. The game will look at a set of desired outcomes—which we will provide—and if the simulation produces such a desired outcome, the face number will be shown. Otherwise, zero will be shown.

Syntax

roll(desired_outcome,die_sides)

Parameters

  • desired_outcome: This is the outcome that we desire from the simulation. This parameter is of the object data type.

  • die_sides: This is an integer data type. This parameter has a default value of 6, as in the sides of a die. But, we have the option to set it to any other value.

Return value

The return value of the roll() method is an integer. This integer will be 0 if the occurrence of any of the desired_outcomes is recorded. Otherwise, the return value will be the face number that was rolled.

Example

The code given below is a sample use of the roll() method.

include std/rand.e
object desired_outcome1, desired_outcome2
integer die_sides = 40
desired_outcome1 = {1,2,3,4,25,40,30,10,14,20,35,15,5}
desired_outcome2 = {1,2,3,4,5,6}
--returns 0 apart from when the die face shows 3
printf(1,"%d\n", roll(3,6))
--we shall always have the returned face shown unlike above
printf(1,"%d\n", roll(desired_outcome2))
--a die with a whooping 40 sides
print(1,roll(desired_outcome1,die_sides))

Explanation

  • Line 1: We include the roll() method using the rand.e module from the standard library.
  • Line 3: We declare some variables.
  • Lines 5–7: We assign values to the variables that we declared.
  • Lines 9–14: We use the roll() method to perform the simulation and print the outcome to the screen, with some comments in between.

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