Updated March 31, 2023
Introduction to Kotlin Random
The kotlin random is one of the abstract class types, and it is implemented with the random number generator algorithms. The companion object also performed this area for the default instance of the Random class; the seeded instance of the random generator will use the random function. The instance of the class will generate the stream of pseudorandom numbers. The two instances of random are created with the same seed, and the same sequence of methods are used and called for generating, and it will return the unique identical sequence numbers util package of a random class called the multithreaded design in the random generator.
Syntax of Kotlin Random
In kotlin language has many default classes, methods and keywords for to implement the application. Like Random is one of the abstract class types, and it is used to generate the pseudo-random number generator; it has its own keyword and its syntax for to perform the operations in the code.
fun name()
Assert.assertTrue((0..1000).all{ //(0..1000) it’s a number range we can fix it with any number ranges
(0..10).contains((0..10).random())
}
)
Assert.assertFalse(((0..1000).all{
(0..10).contains((0..10).random())
}
}
fun main(args:Array<String>)
{
--some logic codes depends on the requirement---
}
The above code is the basic syntax for utilising the random class and its method for to calculate the pseudo-random number generations.
How Random Works in Kotlin?
The random number generation is like a pseudo number sequence; it generates automatically and random manner. It returns a repeatable and sequence order with seeded on the data type like two generators with the same seed produce for sequence number range also the same version of kotlin runtime polymorphism. With the help of a random number generator algorithm, the numbers are calculated, generating sequence. While on the JVM machine, the random number generator is not the thread-safe, and it does not invoke it with the multiple thread sequences; it is also arranged with the synchronization.
So that two seeds can produce with the same sequences of values within the kotlin runtime versions. Based on the algorithm, the user inputs are accepted with initial ranges that can be the seed or key so we apply that seed in the sequence manner with some mathematical operations for to generate the output results. So that output we named it random number use that also results in random number will give the next set of iterations to continue their tasks. So it’s not possible for generating the true random numbers. So it has two types like non-deterministic random number generator and true random number generator. Non-deterministic random number generators are also called as pseudorandom number generators.
Examples of Kotlin Random
Given below are the examples of Kotlin Random:
Example #1
Code:
package one;
import java.util.Random
val rd = Random()
object Tests {
var infos = "Welcome To My Domain its the first example that related to the kotlin random function"
}
fun main(args: Array<String>) {
val vars = arrayOf("'Your first inputs' - Its the Monday", "'The second inputs' - Its the tuesday", "'The third inputs' - Its the Wednesday", "'The fourth inputs' - Its the Thursday", "'The fifth inputs' - Its the Friday", "'The sixth inputs' - Its the Saturday ","'The seventh inputs' - Its the Sunday", "'Every object has the separate attributes and its elements' - Programming language")
val vars1 = arrayOf("'Your eight inputs'- programming concepts and its implemnetations","'The most important things in the life are the day you are born and the day you find out why'- what is the reason for this")
val vars2=arrayOf("'The life is more precious things and don’t wasted every single minute in a day you are born and the day you find out why.' - Great legend","'Great lengends are more powerful when compare to the others life are the day you also born with the silver spoon' - The Legendary things")
fun res(at1: Int, at2: Int) = rd.nextInt(at2-at1+1)+at1
println(vars1[res(0,(vars1.size)-1)])
println(vars[res(0,(vars.size)-1)])
println("Thank you users have a nice day users keep spending your valuable time with us")
}
Output:
In the above example, we used the arrayOf() method in the variable, and we validated the integer number and generate it randomly.
Example #2
Code:
package one;
import java.util.Random
fun demo(strleng: Int) : String {
println("Welcome To My Domain its the second example that related to the kotlin random concepts")
val inpstr = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz0123456789"
return (1..strleng)
.map { inpstr.random() }
.joinToString("")
}
fun second(){
var lst=ArrayList<String>()
lst.add("The input is entered on the text box is: 0")
lst.add("The input is entered on the text box is: 1")
lst.add("The input is entered on the text box is: 2")
lst.add("The input is entered on the text box is: 3")
lst.add("The input is entered on the text box is: 4")
lst.add("The input is entered on the text box is: 5")
lst.add("The input is entered on the text box is: 6")
lst.add("The input is entered on the text box is: 7")
lst.add("The input is entered on the text box is: 8")
lst.add("The input is entered on the text box is: 9")
for(i in lst)
print("The arryList are iterated please find your input values $i ")
}
fun main() {
val strleng = 13
val res = demo(strleng)
println(res)
}
Output:
In the second example, we added collections called arrayList() for to store and retrieve the datas with the random() method.
Example #3
Code:
package one;
import java.util.Random
interface infy {
fun third(strl:String) : String{
println("Welcome To My Domain its the third example that related to the kotlin random concepts")
val inpstr = "Valuable Inputs must be entered and its more important for validation with both front and back end"
return (inpstr)
.map { inpstr.random() }
.joinToString("")
}
}
class Test{
fun four(leng:Int):String
{println("Your operations are starting")
val chs = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..leng)
.map { chs.random() }
.joinToString("")
}
}
fun main() {
val num=17
val strs=Test()
println(strs.four(num))
println("Your operations are ended")
}
Output:
In the final example, we used class and interface are additionally added for to perform the random() function.
Conclusion
In the kotlin language, mobile applications are intermittently added some default methods to perform the user tasks according to the software requirement. But some mathematical operations are needed to be performed without affecting the application functionality like that random is one of the default class and methods for performing these operations.
Recommended Articles
This is a guide to Kotlin Random. Here we discuss the introduction, syntax, and working of random in kotlin along with examples and code implementation. You may also have a look at the following articles to learn more –