# Pseudorandom

**Pseudorandom** numbers are generated by computers. They are not truly random, because when a computer is functioning correctly, nothing it does is random. Computers are deterministic devices — a computer's behavior is entirely predictable, by design. So to create something unpredictable, computers use mathematical algorithms to produce numbers that are "random enough."

## When are pseudorandom numbers used?

Pseudorandom numbers are essential to many computer applications, such as games and security. In games, random numbers provide unpredictable elements the player can respond to, such as dodging a random bullet or drawing a card from a deck.

In computer security, pseudorandomness is important in encryption algorithms, which create codes that must not be predicted or guessed.

## What is a PRNG?

A **pseudorandom number generator**, or **PRNG**, is any program, or function, which uses math to simulate randomness. It may also be called a **DRNG** (digital random number generator) or **DRBG** (deterministic random bit generator).

The math can sometimes be complex, but in general, using a PRNG requires only two steps:

- Provide the PRNG with an arbitrary seed.
- Ask for the next random number.

The seed value is a "starting point" for creating random numbers. The value is used when computing the numbers. If the seed value changes, the generated numbers also change, and a single seed value always produce the same numbers. For this reason, the numbers aren't really random, because true randomness could never be re-created.

The current time is often a unique seed value. For instance, if it's March 5, 2018, at 5:03 P.M. and 7.01324 seconds UTC, that can be expressed as an integer. That precise time never occur again, so a PRNG with that seed should produce a unique set of random numbers.

Being able to reproduce a randomly-generated sequence can be useful. In academic applications, a massive sequence of random values can be generated for a simulation, then reproduced exactly for more detailed analysis later. As another example, in computer games, if a player loads a saved game, any "random" events can be the same as if the game never stopped. That way, the player can't reload the same game repeatedly to try for better luck.

## How to generate pseudorandom numbers

The following are some ways you can create a pseudorandom number in common programs and programming languages.

### Windows Command Prompt

At the Windows command prompt, or in a batch file, the special environment variable **%RANDOM%** produces a pseudorandom number between 0 and 32767, seeded with the time the command prompt launched.

echo "So %RANDOM%!"

"So 27525!"

To create a batch file that generates a random number between 1 and 100:

copy con sorandom.bat echo off set /a myrand=%RANDOM%*100/32768+1 echo The number I was thinking of was %myrand%. Did you get it right?

Press `Ctrl`+`Z` and `Enter` to save the batch file. Then, execute the file:

sorandom

The number I was thinking of was 91. Did you get it right?

### Windows PowerShell

The *Get-Random* cmdlet generates a random number between 0 and 2,147,483,647 (the maximum value of an unsigned 32-bit integer).

Get-Random

1333190525

The cmdlet takes a number of options, such as a minimum and maximum value. Values are rounded down, so to generate a number between 1 and 100, set the maximum to 101:

Get-Random -Minimum 1 -Maximum 101

99

### Microsoft Excel

In an Excel spreadsheet, the formula *=RAND()* generates a random number between 0 and 1. For instance, if you highlight a cell and enter *=RAND()*, the cell generates a number that changes whenever the sheet is re-calculated.

This method also works in other spreadsheet applications, including LibreOffice Calc and Google Sheets.

## In programming languages

Most programming languages have a PRNG functions. Here are some common examples:

### C

In the C programming language, the PRNG functions are defined in the standard library, *stdlib*. The common way to seed the random generator is with the *time()* function, declared in *time.h*. The generated number falls between *0* and the constant *RAND_MAX*, a system-specific integer guaranteed to be at least 32767.

#include <time.h> #include <stdlib.h> #include <stdio.h> void main () { srand(time(NULL)); /* seed the generator */ int rand1 = rand(); /* a pseudorandom integer between 0 and RAND_MAX */ printf("Random number between 0 and %d: %d\n", RAND_MAX, (int)rand1); /* Or, within a specific range: */ int min = 0; int max = 100; float rand2 = (float)rand() * max / RAND_MAX + 1; int round = (int)rand2; printf("Random number between %d and %d: %d (%f)\n", min, max, round, rand2); return; }

Output:

Random number between 0 and 2147483647: 1789080047 Random number between 0 and 100: 74 (74.369179)

### C++

In C++:

#include <ctime> #include <cstdlib> #include <iostream> int main () { srand(time(NULL)); std::cout << "Random number between 0 and " << RAND_MAX << ": " << rand() << "\n" << "Random number between 1 and 100: " << (rand() % 100) + 1 << std::endl; return 0; }

Output:

Random number between 0 and 2147483647: 126569208 Random number between 1 and 100: 9

### Python 3

The *random* module in Python offers various functions for generating random numbers. In this example, we use three different methods for finding a random integer in a range.

import random from datetime import datetime random.seed(datetime.now()) print("Random number in range [0,1): ", random.random()) # Within a range. These all do the same thing: print("Random number between 1 and 100: ", round(random.random() * 100) + 1) print("Random number between 1 and 100: ", random.randrange(1, 101)) print("Random number between 1 and 100: ", random.randint(1, 100))

Output:

Random number in range [0,1): 0.05137418896158319 Random number between 1 and 100: 27 Random number between 1 and 100: 80 Random number between 1 and 100: 80

### Perl 5

In Perl:

srand(time); # changes once per second print "Random number in range [0,1): ", rand(), "\n"; print "Random number in range [1, 100]: ", int(rand(101)), "\n";

Output:

Random number in range [0,1): 0.691379946963028 Random number in range [0, 100]: 82

### JavaScript

console.log("Random number in range [0,1): " + Math.random()); console.log("Random number in range [1,100]: " + Math.floor(Math.random() * 101));

View the output in your web browser's JavaScript console (for instance, in Firefox press *Ctrl*+*Shift*+*K*):

Random number in range [0,1): 0.305008216755414 Random number in range [1,100]: 8

It's not possible to seed the **Math.random()** function in JavaScript. If you need a robust PRNG in JavaScript, check out better random numbers for JavaScript on GitHub.

## Example PRNG: JavaScript widget

Using the widget below, you can seed a PRNG and use it to generate random numbers.

Each time you generate a random number from your given seed, its offset increases by 1. The first number generated from the seed has offset zero, the second has offset 1, etc. The generator always produces the same number for a given seed and offset.

Enter anything you want into the field to create a unique seed.

Use the Generate button to get the next random number using that seed, and increment the offset.

Use the Reset button to reset the offset to zero.

** number generated from seed ****: **

This widget uses Johannes Baagøe's open source PRNG scripts, Alea.js and Mash.js.

Computer security, Mersenne Twister, Monte Carlo method, Programming, Proof-of-Stake, Software terms