When Random Numbers Are Not Random 
Tags: recap, computerscience, codenewbie, concepts
Published: 2021-08-16 22:25:56

"And then we can have it spit out something random!"
![What is RNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goj2j9j56c6vk01cw880.png)I think every brainstorming session I've had has thrown that idea out. Both in the company of my code collaborators or the solitude of a personal project, the utility of random number is super handy. Random numbers are essential in forms of cryptography and eliminating as much bias as possible.

But how do we know a random number is random? Aren't machines programmed to do specific instructions, so isn't that mean that no number is random? If computers only follow instructions, can they really guess?![RNG Computer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hn1xxgtz1qnvo0o1l2r3.png)
[This video](https://www.youtube.com/watch?v=Ac4Z1VMsE3E&ab_channel=ArtoftheProblem) goes into the main ideas of how computers choose randomness.



Computers use a trick when they are told to select a random number. They grow randomness a _"**seed**"_- a number taken from an unpredictable source.
![RNG Basics](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mf6hqbjdb20rqjv8pduj.png)
A common place to generate a seed from is using time or CPU clocks.

That _**seed**_ then needs to grow- and computers grow the seed by running it through an algorithm.

{% youtube Ac4Z1VMsE3E %}

Here's an example method called **"middle squares"**, credited to [John Von Neumann](https://mathshistory.st-andrews.ac.uk/Biographies/Von_Neumann/). 

- A Seed Is Established

_Example:_
```seed = 173```

- Seed Is Multiplied By Itself

_Calculation:_
```seed*seed = result```

_Example:_
```173*173 = 29929```

- Output The Middle Of The Result
_Example:_        
```output = 992```

- Multiply The Result By The Result
_Repeated Calculation:_        
```result*result = newResult```

_Example:_      
```29929*29929 = 895745041```

- Output The Middle Of The New Result
_Example:_       
```output = 992745```

- Repeat Each Calculation With Each New Result
_Example:_        
```output = 992745847...```

This is or a variation on these principles dictate how most random numbers are generated. But is this cryptographically sound?

![Cryptographically Secure RNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zv3ah1emexdn8ynpaq9.png)

Recently I was at a talk featuring [The First Prototype](https://thefirstprototype.com/)'s founder, [Saamer Mansoor](https://www.linkedin.com/in/saamer/), who lead such an enthusiastic and interesting lecture on the subject of randomness and finding cryptographically secure RNG.

Trends in the forefront of the wider world focus in on data leaks, institutional skepticism, and ransomware.

Luckily there are thousands of [Developers](https://webhome.phy.duke.edu/~rgb/General/dieharder.php), [Scientific Laboratories](https://www.nist.gov/cyberframework), [Security Agencies](https://www.intelligencecareers.gov/nsa/nsacareerdevelopment.html), and [NGOs](https://en.wikipedia.org/wiki/Cryptography_standards) working tirelessly to develop safe and sustainable means of encryption, together.

{% youtube tP-Ipsat90c %}

There are good things about unsecure RNG. A friend of mine once shared how a company under a ransomware attack were able to generate a key to unlock their hijacked data- the hackers had generated a random number to create the key, but because it was based off of a CPU clock, the company was able to recreate the seed and regain control of their data.

Most of this is not super important or pertinent to junior developers, but it's always good to cover what's going on in the world.

As junior developers, it's always good to go over your numbers.

[![LinkTree](https://img.shields.io/badge/LINKTR.EE/%20-darkgreen?&style=for-the-badge&logo=linktree&logoColor=white)](https://linktr.ee/jacksonrgoodman)
[![LinkedIn](https://img.shields.io/badge/Linked%20In-0e76a8?style=for-the-badge&labelColor=0e76a8&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/jacksonrgoodman/)
[![Github](https://img.shields.io/badge/Github-000000?style=for-the-badge&labelColor=000000&logo=github&logoColor=white)](https://github.com/jacksonrgoodman)

![Code Quest](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c19aawnx52r4qq8l1wlb.png)