Random number acquisition in various languages


In the past, I summarized how to get the current time, but I felt that there are many cases of acquiring random numbers, so I decided to summarize how to get integers between 0 and 10 as pseudo-random numbers. In addition, since the algorithm of random number generation is profound, the method introduced here is not particular about quality, but it is just a routine personal memo of "If you want to make random numbers easily, do this".


At first, it was in C language. Because if you understand how to get random numbers in C, you can grasp the general outline that "this procedure is necessary" in almost any language.

The rand()function can be used to obtain integers greater than RAND_MAX 0 and less than or equal to 0. However, if the seed of the random number is not intentionally set in the srand()function, 1 is assumed as the seed, so the same random number is generated each time it is executed. A common way to avoid this is to set the current time as the seed.

  1. Get the current time (elapsed seconds)
  2. srand()Setting the current time to seed in a function
  3. rand()Get random numbers in a function
  4. Arithmetic operations on acquired random numbers such as remainder and fit them within the required range

 * min以上max未満の整数をランダムに取得する
int getRandomInt(int min, int max){
    return rand() % (max - min) + min;

int main(void){
", getRandomInt(0, 10));
    return 0;


Java has a java.util.Random class, so it's easy to use.

  1. java.util.RandomInstantiate a class
  2. nextInt()Get random numbers in a method
import java.util.Random;
public class Sample{
  public static void main(String[] args){
    Random r = new Random();//引数にシードを設定することも可。省略するとSystem.nanoTimeに応じて決定される値が仮定される

In addition, there is a random()method in the java.lang.Math class, so there is a way to use it, but the implementation example is similar to JavaScript, so it is omitted.

C #

Almost the same as Java. Easy to use System.Random class.

using System;
namespace Sample{
    class Program{
        static void Main(string[] args){
            Random r = new Random();//引数にシードを設定することも可。省略するとEnvironment.TickCountに応じて決定される値が仮定される


In the case of JavaScript, use Math.random(). However, this function is called

  • Return a decimal greater than or equal to 0.0 but less than 1.0
  • Unable to set seed
 * min以上max未満の整数をランダムに取得する
function getRandomInt(min: number, max: number): number {
    min = Math.ceil(min);   //その数以上の最小の整数にする。1.23 → 2
    max = Math.floor(max);  //その数以下の最大の整数にする。1.23 → 1
    return Math.floor(Math.random() * (max - min)) + min;


Go uses the Math/rand package. The seed is rand. It is set with Seed(), but since the default is assumed to be 1, it is common to intentionally set it with the current time etc. as in C language. Unlike C, if you use Intn(), you don't need to take a remainder.

package main
import (
func main(){
    rand.Seed(time.Now().UnixNano()) //経過秒数をシードに設定
    fmt.Println(rand.Intn(10)) // 0以上10未満の整数を取得


Easy to use the Random class. Whether the value specified for rand()is Int or Float or 1. Note that the movement changes depending on whether the range is specified such as 10. You can also set the seed with srand()like C.

r = Random.new()  # 引数にシードを設定する。省略するとRandom.new_seedが仮定される
puts r.rand(10)   # 0以上10未満の整数を取得
puts r.rand(10.0) # 0.0以上10.0未満の小数を取得


It is easy to use the random module, but be careful because random.randrange() is less than and random.randint() is the following and the functions that are easy to misuse are lined up. If you want to set the seed, intentionally set random.seed. If not set, the system time is assumed. For details, please refer to the reference site.

import random
print(random.randrange(0, 10))  # 0以上10未満の整数を取得
print(random.randint(0, 10))  # 0以上10 以 下 の整数を取得


Like Java, use the Random class. Use this when you want to set the seed.

import kotlin.random.Random
fun main(args: Array<String>) {
    println(Random.nextInt(10))  //0以上10未満の整数を取得


Random API has appeared since swift4.2, so it is easy to use.

print(Int.random(in: 0..<10)) //0以上10未満の整数を取得

Shell Scripts

If you are using bash or zsh, the shell variable$RANDOM contains random numbers. Windows bat has%RANDOM%as well.

echo $RANDOM


In the case of a database that provides a SQL function that can generate random numbers, it is also possible to obtain random numbers with SQL.


RAND()can be used to obtain decimals between 0.0 and 1.0, so you can perform arithmetic operations on them and fit them within the necessary range.



Use RANDOM(). MySQL and function names are different because they are the same thing, so I omit them.


For the time being, I tried to summarize how to get random numbers for languages that I have been involved in. In the future, if random numbers are required in another language, we will update them from time to time.

Author by


某大手企業の社畜プログラマー C/Java/C#/JavaScript/Kotlin/Swift→現在に至る

Updated on May 05, 2019