## Introduction to Scala Random

Scala Random function is used to generate random numbers or characters in Scala. To generate the Random numbers over Scala we use the Random function with the Scala.util.Random class. The Random number generated can be anything be it Integer, Float, Double, char. This random no is used and important for various level applications such as Validation, etc. So it is used to Random generation of numbers in our Scala Application.

**Syntax:**

The Scala.util.Random class is used for the generation of Random numbers over the application. We just need to import the class and the methods like nextInt,nextFloat generates the numbers.

`val r = scala.util.Random`

r: util.Random.type = scala.util.Random$@7889a1ac

r.nextInt

res1: Int = 1825881164

r.nextInt(100)

res2: Int = 99

r.nextFloat

res3: Float = 0.10983747

### Working of Scala Random with Examples

The Scala Random function takes up the random function to generate numbers for processing, it generally uses the Linear congruential generator, this algorithm works on choosing the random number. We can also select the range over which we want to generate the number over. We will see the random number generation with some examples.

#### Example #1

**Code:**

`val r = scala.util.Random`

r: util.Random.type = scala.util.Random$@7889a1ac

r.nextInt

res0: Int = 2123631858

r.nextInt

res1: Int = -737405300

r.nextInt

res2: Int = 377538368

Every Time the code is run or this function is called we will get the set of different values in the usual pattern without following any rule or pattern. Even we can also set an inclusive as well as an exclusive limit for the random number we want to generate.

4.5 (7,258 ratings)

View Course

**Note:** By default, the inclusive limit is 0 so we can also set only the exclusive one.

`r.nextInt(100)`

res3: Int = 64

r.nextInt(100)

res4: Int = 91

r.nextInt(100)

res5: Int = 39

r.nextInt(100)

res6: Int = 38

**Output:**

#### Example #2

We can also select the Random Float value by the method NextFloat.So the range will lie from 0.0 decimal value to 1. Let us check that with some example:

**Code:**

`r.nextFloat`

res8: Float = 0.59556204

r.nextFloat

res9: Float = 0.8322488

r.nextFloat

res10: Float = 0.6295014

r.nextFloat

res11: Float = 0.69067985

r.nextFloat

res12: Float = 0.7225474

r.nextFloat

res13: Float = 0.9606658

r.nextFloat

res14: Float = 0.77049905

Same as Float we can also create random numbers for Double Values.

`r.nextDouble`

res18: Double = 0.34614360601266014

r.nextDouble

res19: Double = 0.38648718502076507

r.nextDouble

res20: Double = 0.31311541536121046

r.nextDouble

res21: Double = 0.7410149595118738

It also prints the values between 0 to 1. The Boolean value can also use the same Random value and yields result based on Boolean Values.

`r.nextBoolean`

res15: Boolean = true

r.nextBoolean

res16: Boolean = false

r.nextBoolean

res17: Boolean = false

**Output:**

#### Example #3

Even the Scala Random function is used to Randomly generate the characters also. We can use the method nextprintableChar from the Random class method and generates characters accordingly. Let us check that with Example:

**Code:**

`r.nextPrintableChar`

res24: Char = K

r.nextPrintableChar

res25: Char = g

r.nextPrintableChar

res26: Char = k

r.nextPrintableChar

res27: Char = K

r.nextPrintableChar

res28: Char = '

r.nextPrintableChar

res29: Char = t

So it prints all the CharatcersRandomly. It is possible that the same character or integer value can come at many times, there is no rule for the numbers not to be repeated.

Note: This Random function is well known used with the Pattern Verification and security purpose like of Captcha and all.

**Output:**

#### Example #4

We can also use the Random function over a distribution. One known function that works with it is the Gaussian function. The Gaussian Function takes the Random data over the Gaussian Distribution and prints data accordingly. It returns a random number with a mean of 0 and a deviation of 1. To change this Gaussian value we need to provide that explicitly.

**Code:**

`r.nextGaussian`

res35: Double = 1.301074019733114

r.nextGaussian

res36: Double = 0.37365693728172494

r.nextGaussian

res37: Double = -0.2868649145689896

r.nextGaussian

res38: Double = 2.108673488282321

**Output:**

This is what it generates randomly over a Gaussian Distribution.

#### Example #5

We can also merge random functions and create a List or store them in the collection we want to. Let us check that with Example:

**Code:**

`for(i<- 0 to r.nextInt(4)) yield r.nextDouble`

res40: scala.collection.immutable.IndexedSeq[Double] = Vector(0.020069508131527525)

for(i<- 0 to r.nextInt(4)) yield r.nextDouble

res41: scala.collection.immutable.IndexedSeq[Double] = Vector(0.6992494049547558)

for(i<- 0 to r.nextInt(10)) yield r.nextDouble

res42: scala.collection.immutable.IndexedSeq[Double] = Vector(0.9844960499444084, 0.06772285166187964, 0.9797605964534618, 0.6239437080597234, 0.015670036830630618, 0.8530556031658404)

for(i<- 0 to r.nextInt(10)) yield r.nextDouble

res43: scala.collection.immutable.IndexedSeq[Double] = Vector(0.0775137969760199, 0.3150585897780521, 0.5429361580144657, 0.7427799136029297, 0.7595647379710992, 0.6097524030728557, 0.5555829149364843, 0.031480808153179884, 0.9486129909099824, 0.1519146584718376)

for(i<- 0 to r.nextInt(10)) yield r.nextPrintableChar

res44: scala.collection.immutable.IndexedSeq[Char] = Vector(Q, q, n, ", [, r, K, 0, B)

for(i<- 0 to r.nextInt(10)) yield r.nextPrintableChar

res45: scala.collection.immutable.IndexedSeq[Char] = Vector(%, ?)

for(i<- 0 to r.nextInt(10)) yield r.nextPrintableChar

res46: scala.collection.immutable.IndexedSeq[Char] = Vector(m, =)

for(i<- 0 to r.nextInt(3)) yield r.nextPrintableChar

res47: scala.collection.immutable.IndexedSeq[Char] = Vector(6)

for(i<- 0 to r.nextInt(10)) yield r.nextPrintableChar

res48: scala.collection.immutable.IndexedSeq[Char] = Vector([, =, V, !, Q, f, 9, E)

Here we can see how we merged the different functions of Random and generated results accordingly.

**Output:**

Hence Scala Random function is used for generating Random values over the Scala Application that can be needed over again and again.

### Conclusion

From the above article, we saw how we can use the scala Random function to generate random values and use it over the Scala Application. We also saw the various type by which we can create a random number. So it is a very good and important method used in Scala Programming for various scala works.

### Recommended Articles

This is a guide to Scala Random. Here we also discuss the introduction and working of scala random along with different examples and its code implementation. You may also have a look at the following articles to learn more –