# Random number acquisition in various languages

1

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".

# C

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 s`rand()`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
``````#include<stdio.h>
#include<stdlib.h>
#include<time.h>

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

int main(void){
srand((unsigned)time(NULL));//経過秒数でシードを設定する
printf("%d\
", getRandomInt(0, 10));
return 0;
}
``````

# Java

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

1. `java.util.Random`Instantiate 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に応じて決定される値が仮定される
System.out.println(r.nextInt(10));//0以上10未満の整数を取得
}
}
``````

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に応じて決定される値が仮定される
Console.WriteLine(r.Next(10));//0以上10未満の整数を取得
}
}
}
``````

# JavaScript(TypeScript)

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;
}
``````

# golang

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 (
"fmt"
"math/rand"
"time"
)
func main(){
rand.Seed(time.Now().UnixNano()) //経過秒数をシードに設定
fmt.Println(rand.Intn(10)) // 0以上10未満の整数を取得
}
``````

# Ruby

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 s`rand()`like C.

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

# Python

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 以 下 の整数を取得
``````

# kotlin

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未満の整数を取得
}
``````

# swift

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
``````

# SQL

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.

## MySQL

`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.

``````SELECT FLOOR((RAND() * 10))
``````

## PostgreSQL

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

# summary

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.

Share:
1 Author by

### 議長

Updated on May 05, 2019