seoh / pseudo-random-yellowbook

A yellow-book of pseudo-random generation of modern programming languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pseudo-random-yellowbook

A yellow-book of pseudo-random number generation of modern programming languages.

List ordering follows TIOBE Index.

Language Module 1 <= integer <= 10 0 <= float < 100
C stdlib.h rand() % 10 + 1 rand() / (float)RAND_MAX * 100
Python random random.randint(1, 10) random.random() * 100
Java java.util.Random new Random().nextInt(10) + 1 new Random().nextFloat() * 100
C++ <random> std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<int> dist(1, 10); dist(gen); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<float> dist(0.0f, 100.0f); dist(gen);
C# System new Random().Next(1, 11) new Random().NextDouble() * 100
Visual Basic VBMath CInt(Int((Rnd() * 10) + 1)) Rnd() * 100
JavaScript Math Math.floor(Math.random() * 10) + 1 Math.random() * 100
Assembly
PHP Math (core) mt_rand(1, 10) mt_rand() / (float)(mt_getrandmax() + 1) * 100
SQL - TRUNC(RANDOM() * 10) + 1 RANDOM() * 100
Ruby Random rand(10) + 1 rand * 100
Go math/rand rand.Intn(10) + 1 rand.Float64() * 100
Swift Swift Standard Library Int.random(in: 1...10) Float.random(in: 0..<100)
MATLAB intrinsic randi(10) rand * 100.0
Fortran intrinsic CALL RANDOM_NUMBER(r); i = ceiling(r*10) CALL RANDOM_NUMBER(r); r = r*100
R base, stats sample(1:10, 1) runif(1, 0, 100)
Perl
Delphi
Rust rand::Rng rand::thread_rng().gen_range(1..=10); rand::thread_rng().gen_range(0.0..100.0) as f32;
Julia Random abs(rand(UInt64)) % 10 + 1 abs(rand(Float64)) * 100
Scala scala.util.Random Random.nextInt(10) + 1 Random.between(0f, 100f)
Clojure clojure.core/rand clojure.core/rand-int (+ (rand-int 10) 1) (rand 100)
Bash $RANDOM $(( $RANDOM % 10 + 1 )) $(( $RANDOM / 32768.0 * 100 ))

SQL

Random function is not present in ANSI SQL standard and should be specified in accordance with respective implementations.

Fortran

  • i is INTEGER and r is REAL.
  • Declare output variable first and pass it to RANDOM_NUMBER.
      PROGRAM test_random_number
          REAL :: r
          CALL RANDOM_NUMBER(r)
      END PROGRAM

Bash

$RANDOM returns an integer in the range 0 ~ 32767. To get a floating number in range of [0..1), divide it by 32768.

About

A yellow-book of pseudo-random generation of modern programming languages

License:The Unlicense