Kotlin - Random and Secure Random with Example - Generate random number in kotlin

Random

The classes defined in Random are not cryptographically vigorous, and the numbers culled are not consummately random because a definite mathematical algorithm is utilized to cull them. Ergo, it is not safe to utilize this class for tasks that require a high calibre of security, like engendering an arbitrary password, etc.
The class utilizes a 48-bit seed
Instances of java.util.Random are threadsafe. However, the concurrent utilization of the same java.util.Random instances across threads may encounter contention and consequent poor performance. Consider instead utilizing ThreadLocalRandom in multithreaded designs.
Instances of java.util.Random is not cryptographically secure. Consider instead utilizing SecureRandom to get a cryptographically secure pseudo-desultory number engenderer for use by security-sensitive applications. 

Random Example:

package com.knowledgefactory

import java.util.*

/*
* A Kotlin program to demonstrate random number
generation using java.util.Random;
import java.util.Random;
* */
object KnowledgeFactoryRandom {
@JvmStatic
fun main(argv: Array<String>) {
// create instance of Random class
val rand = Random()
// Generate random integers in range 0 to 9999
val value = rand.nextInt(10000)
// Print random integers
println("Random Integers:$value")
}
 

SecureRandom

This class provides a cryptographically random arbitrary number engenderer (RNG).
A cryptographically vigorous random number minimally complies with the statistical desultory number engenderer tests designated in FIPS 140-2, Security Requisites for Cryptographic Modules, section 4.9.1. Supplementally, SecureRandom must engender non-deterministic output. Consequently any seed material passed to a SecureRandom object must be capricious, and all SecureRandom output sequences must be cryptographically vigorous.
A caller obtains a SecureRandom instance via the no-argument constructor or one of the getInstance methods:

SecureRandom arbitrary = incipient SecureRandom();  

SecureRandom Example:

import java.security.SecureRandom

/*
* A Java program to demonstrate random number
* generation using java.security.SecureRandom ;
* import java.security.SecureRandom ;
* */
/**
* @author www.knowledgefactory.net
*/
object KnowledgeFactorySecureRandom {
@JvmStatic
fun main(argv: Array<String>) {
// create instance of SecureRandom class
val rand = SecureRandom()
// Generate random integers in range 0 to 9999
val value = rand.nextInt(10000)
// Print random integers
println("Random Integers:$value")
}
}
 


More Kotlin related topics,

Kotlin + Spring Boot CRUD examples:

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example