# What is pseudo-random?

Updated: 01/24/2018 by Computer Hope

Pseudo-random 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 pseudo-random numbers used?

Pseudo-random 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 the top of a deck.

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

## What is a PRNG?

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

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

1. Provide the PRNG with an arbitrary seed.
2. 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 will 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 used as a unique seed value. For instance, if it’s March 5, 2018, at 5:03 PM and 7.01324 seconds UTC, that can be expressed as an integer. That precise time will never occur again, so a PRNG with that seed should produce a unique set of random numbers.

Note: 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 just reload the same game repeatedly to try for better luck.

## How to generate pseudo-random number

The following are some ways you can create a pseudo-random 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% will produce a pseudo-random number between 0 and 32767, seeded with the time when the command prompt was 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() will generate a random number between 0 and 1. For instance, if you highlight a cell and enter =RAND(), the cell will contain a number that will change 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 their own 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, defined in time.h. The number generated 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: %dn", RAND_MAX, (int)rand1);
/* Within a 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 a variety of 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));
```

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

Note: 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.

Software terms, Computer security, Programming

Top
• b
• a
• c
• j
• F