Generate Random Integer Between Two Values in Scala

Generate Random Integer Between Two Values in Scala

Hi,

I am working on the SCALA project and want to generate random integer between two numbers.

How to Generate Random Integer Between Two Values in Scala?

Thanks

View Answers

November 13, 2017 at 12:38 PM

Hi,

Here is code example:

import scala.util.Random
val randomGenerator = new Random
val start = 20
val end = 70
val genValue = start + randomGenerator.nextInt( (end - start) + 1 )

The scala.util.Random class is used for generating random number.

If you run the code it will give following output:

scala> import scala.util.Random
import scala.util.Random

scala> val randomGenerator = new Random
randomGenerator: scala.util.Random = [email protected]

scala> val start = 20
start: Int = 20

scala> val end = 70
end: Int = 70

scala> val genValue = start + randomGenerator.nextInt( (end - start) + 1 )
genValue: Int = 62

scala>

Thanks

Ads









Related Tutorials/Questions & Answers:
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Advertisements
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
generate random integer matrix in r
generate random integer matrix in r  Hi, I want to generate random integer matrix in r. What is easy code to generate random integer matrix in r? Thanks   Hi, Following is the code for generating random integer
Default constructor generate two random equations
Default constructor generate two random equations  Need to create a default constructor in a programmer defined class that generates a random question, addition or subtraction. And when adding the numbers must be random from 0-12
How to store two integer value in a separate file and retrieve those values
How to store two integer value in a separate file and retrieve those values  I need to save two values so i can use those values later i came up with idea of saving those values in a separate file and updating values in file
Generate random numbers in Java
be used to generate a random number between 0,1000: int randomInt = randomGenerator.nextInt(1000); Following is the code to generate 10 random number between 0...Generate random numbers in Java - How to use the java.util.Random class
How to generate a list of random numbers?
of Scala programming and it generates random integer. Following program...How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  
How to generate a list of random numbers?
of Scala programming and it generates random integer. Following program...How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  
How to generate a list of random numbers?
of Scala programming and it generates random integer. Following program...How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  
How do I generate random number?
How do I generate random number?  In my Java program there is a requirement of generating random numbers between two given numbers. How do I write..., The java.util.Random class can be used to generate the random numbers. Check the tutorial
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
How to generate random number in java
How to generate random number in java In this section you will learn how to generate random number in java. Java API  provide a random class in java.util.Random package which generate random number within a range. The random
Hibernate Built-in criterion "between" Integer
Hibernate Built-in criterion "between" Integer In this tutorial you will learn about how to use the built-in criterion ' between ' with the integer values. Hibernate provides the facility to make a Criteria query
Populating a list is Scala with random double
Populating a list is Scala with random double  Hi, I am writing Big Data Analytics program and there is need of populating random double values. Is there any easy for this? I want code for Populating a list is Scala with random
Populating a list is Scala with random double
Populating a list is Scala with random double  Hi, I am writing Big Data Analytics program and there is need of populating random double values. Is there any easy for this? I want code for Populating a list is Scala with random
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using... creates an array and fills it with random ints, prints the contents... ArrayRandom{ public static void main(String args[]){ Random r = new Random
ModuleNotFoundError: No module named 'random_string_generate'
ModuleNotFoundError: No module named 'random_string_generate'  Hi...: No module named 'random_string_generate' How to remove the ModuleNotFoundError: No module named 'random_string_generate' error? Thanks   Hi
ModuleNotFoundError: No module named 'random_string_generate'
ModuleNotFoundError: No module named 'random_string_generate'  Hi...: No module named 'random_string_generate' How to remove the ModuleNotFoundError: No module named 'random_string_generate' error? Thanks   Hi
Generate random numbers from 1 to 100
Generate random numbers from 1 to 100  1)A class Called: RandomNumberGenerator that generate random numbers from 1 to 100 2)A class Test that tests... an object from the Random NumberGenerator class to generate your input test data
SQL BETWEEN Operator
of data between two values. In SQL, the BETWEEN operator is used to select a range of data between two values. These values can be number, text and dates. ... The SQL BETWEEN operator helps you to return a records between two
calculate difference between two dates
calculate difference between two dates  hi, I was actually working on to calculate the number of days between two dates of dd/mm/yyyy format... the difference between the two dates
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ... between two date. The syntax of DATEDIFF is .. SELECT DATEDIFF('2012-01-31 23:59
calculate difference between two time in jsp
calculate difference between two time in jsp  How to calculate difference between two dates
java collision detection between two images
java collision detection between two images  java collision detection between two images
Collision Detection between two images in Java
Collision Detection between two images in Java  Collision Detection between two images in Java
Hibernate's Built-in criterion: Between (using Integer)
Hibernate's Built-in criterion: Between (using Integer... will learn to use "between" with the Integer class. "Between" when used with the Integer object, It takes three parameters e.g.  between("
how to get string between two characters
how to get string between two characters  hgow to get string from between two characters. ex:[email protected] from the above word i want a string between characters . and @ send the required method and give better example

Ads