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:

Comments

Popular posts from this blog

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

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String