Skip to content

Commit

Permalink
Added Random Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Neonsy committed Jun 12, 2024
1 parent 243b90d commit 62c6bc1
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 6 deletions.
10 changes: 6 additions & 4 deletions Docs/docs/JavaScript/Notes/Basics/Math.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ toc_max_heading_level: 4
slug: /JavaScript/Basics/Math
sidebar_label: Math
pagination_prev: JavaScript/Notes/Basics/Datatypes
pagination_next: null
pagination_next: JavaScript/Notes/Basics/Random Numbers
---

In JavaScript, there is an `object` called `Math` that provides a set of helpful mathematical functions. This `Math` object is available everywhere in your code, so you can use it anytime you need to perform a mathematical operation.
Expand Down Expand Up @@ -174,7 +174,9 @@ ObjectName.functionName() // To call a function
Math.random(); // e.g., 0.123456789
```

## Next T.B.D
## Next

Now as I've stated at the beginning of this topic / section, this is a quick overview of the Math object.
There's a lot more to it, but these are things that one would probably use the most.
As stated at the beginning of this topic / section (I still havent decided what's what), this is a quick overview of the Math object.
There's a lot more to it, but these are things that one would probably use the most.

Now that we have the basics of Math down, we can start looking at generating random numbers.
124 changes: 124 additions & 0 deletions Docs/docs/JavaScript/Notes/Basics/Random Numbers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: Random Numbers
description: Find out how to generate random numbers in JavaScript.
slug: /JavaScript/Basics/Random Numbers
pagination_prev: JavaScript/Notes/Basics/Math
pagination_next: null
---

Now first things first, you need to understand that there are no `truly` random numbers but rather `pseudo-random` numbers.

That's because computers are deterministic machines, meaning that they always follow the same rules and produce the same output for the same input.

The core reason why there are no true random numbers in most if not all programming languages is that they use pseudo-random number generators (PRNGs), which rely on deterministic algorithms and an initial seed value to produce numbers.

These numbers only appear random but are ultimately predictable if you know the algorithm and the seed.

True randomness, which is completely unpredictable, typically requires hardware-based generators that rely on physical processes, not just algorithms.

## What do we need?

In order to generate random numbers in JavaScript, we need to use the `Math.random()` function.

This function returns a pseudo-random number between 0 and 1.

### How do we use it?

Because `Math.random()` returns a number between 0 and 1, we need to use `Math.floor()` to round it down to the nearest whole number.

On top of that, we can define a range for the random number to be generated in.

## Examples

In most cases you'd want a whole number, so the examples below will use `Math.floor()` to round the random number down to the nearest whole number.

### Generate a random number

```js title="Random Numbers.js"
let randomNumber = Math.floor(Math.random());
console.log(randomNumber);
```

:::info
The `Math.floor()` function is used to round a number down to the nearest whole number.

Because `Math.random()` returns a number between 0 and 1, it'll always result in 0 for this example.
:::

### Random number with max value

In order to do this, we need to multiply the result of `Math.random()` by the `max` value we want the random number to be generated in.

It is important to note that this multiplication should happen before the `Math.floor()` function is used.

```js title="Random Numbers.js"
let max = 10;

let randomNumber = Math.floor(Math.random() * max);
console.log(randomNumber);
```

:::info
The example will generate a random number between 0 and 9, because `Math.random()` returns a number between 0 and 1.
:::

### Random number with a specific start value

To make the random number start at a specific value, we can use the `Math.floor()` function again, but this time we'll add an `offset` value to the result of `Math.floor()`.

```js title="Random Numbers.js"
let max = 10;
let offset = 1;

let randomNumber = Math.floor(Math.random() * max) + offset;
console.log(randomNumber);
```

This will generate a random number between 1 and 10.
That's because the `offset` value is added to the result of `Math.floor()`, which as seen above, will always result in a number between 0 and 9.

:::info
It is important to understand the order of operations.

1. We get a random number between 0 and 1.

For example: 0.9528724916378934

2. We multiply this number by the `max` value we want the random number to be generated in.

For example: 0.9528724916378934 * 10 = 9.528724916378934

3. We round this number down to the nearest whole number. This is done to get the correct number.
If we were to round up, we would get a number that is outside the range we want the random number to be generated in.

For example: 9.528724916378934 -> 9

4. We add the offset value to the result of `Math.floor()`.

For example: 9 + 1 = 10

That's why the offset will modify from the default `0-9` to the `1-10` range.
:::

### Random number with a specific range

If we combine what we've learned so far, we can generate a random number between a specific range.

This is done by multiplying the random number by the difference between the `max` and `min` values, and then adding the `min` value as the `offset`.

Why do we have to subtract the `min` value from the `max` value?

That's because we have to take into account, that we are not starting from `1`, but from `min`.

```js title="Random Numbers.js"
let min = 50;
let max = 100;

let randomNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(randomNumber);
```

## Next T.B.D

So, I hope this wasn't too confusing.
I recommend you to play around with random numbers, until it's so trivial that you can do it without thinking about it.
2 changes: 1 addition & 1 deletion Docs/src/pages/js-roadmap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Some modules may merge, others might end up on the blog.
- [x] Datatypes
- [x] Math object
- [x] Output
- [ ] Random number generator
- [x] Random number generator
- [ ] Type conversion
- [x] Variables
- [ ] Working with Strings
Expand Down
85 changes: 85 additions & 0 deletions Notes/JavaScript/Basics/Random Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Now first things first, you need to understand that there are no `truly` random numbers but rather `pseudo-random` numbers.
// That's because computers are deterministic machines, meaning that they always follow the same rules and produce the same output for the same input.

/*
The core reason why there are no true random numbers in most if not all programming languages
is that they use pseudo-random number generators (PRNGs),
which rely on deterministic algorithms and an initial seed value to produce numbers.
These numbers only appear random but are ultimately predictable if you know the algorithm and the seed.
True randomness, which is completely unpredictable, typically requires hardware-based generators
that rely on physical processes, not just algorithms.
*/

// ## What do we need?
// In order to generate random numbers in JavaScript, we need to use the `Math.random()` function.
// This function returns a pseudo-random number between 0 and 1.

// ### How do we use it?
// Because `Math.random()` returns a number between 0 and 1, we need to use `Math.floor()` to round it down to the nearest whole number.
// On top of that, we can define a range for the random number to be generated in.

// ## Examples
// In most cases you'd want a whole number, so the examples below will use `Math.floor()` to round the random number down to the nearest whole number.

// ### Generate a random number
let randomNumber = Math.floor(Math.random());
console.log(randomNumber);

/*
The `Math.floor()` function is used to round a number down to the nearest whole number.
Because `Math.random()` returns a number between 0 and 1, it'll always result in 0 for this example.
*/

// ### Random number with max value
// In order to do this, we need to multiply the result of `Math.random()` by the `max` value we want the random number to be generated in.
// It is important to note that this multiplication should happen before the `Math.floor()` function is used.

let max = 10;
randomNumber = Math.floor(Math.random() * max);
console.log(randomNumber);

/*
The example will generate a random number between 0 and 9,
because `Math.random()` returns a number between 0 and 1.
*/

// ### Random number with a specific start value
// To make the random number start at a specific value, we can use the `Math.floor()` function again,
// but this time we'll add an `offset` value to the result of `Math.floor()`.

let offset = 1;
randomNumber = Math.floor(Math.random() * max) + offset;
console.log(randomNumber);

/*
This will generate a random number between 1 and 10.
That's because the `offset` value is added to the result of `Math.floor()`,
which as seen above, will always result in a number between 0 and 9.
It is important to understand the order of operations.
1. We get a random number between 0 and 1.
For example: 0.9528724916378934
2. We multiply this number by the `max` value we want the random number to be generated in.
For example: 0.9528724916378934 * 10 = 9.528724916378934
3. We round this number down to the nearest whole number.
This is done to get the correct number.
If we were to round up, we would get a number that is outside the range we want the random number to be generated in.
For example: 9.528724916378934 -> 9
4. We add the offset value to the result of `Math.floor()`.
For example: 9 + 1 = 10
That's why the offset will modify from the default `0-9` to the `1-10` range.
*/

// ### Random number with a specific range
// If we combine what we've learned so far, we can generate a random number between a specific range.
// This is done by multiplying the random number by the difference between the `max` and `min` values, and then adding the `min` value as the `offset`.

// Why do we have to subtract the `min` value from the `max` value?
// That's because we have to take into account, that we are not starting from `1`, but from `min`.

let min = 50;
max = 100;
randomNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(randomNumber);
3 changes: 2 additions & 1 deletion Notes/JavaScript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ I'm going to order them based on the logical progression and importance, but fee
4. [Operators: Numbers](./Operators/Numbers.js)
5. [Input](./Basics/Input.js)
6. [Datatypes](./Basics/Datatypes.js)
7. [Math (Object)](./Basics/Math.js)
7. [Math (Object)](./Basics/Math.js)
8. [Random Numbers](./Basics/Random%20Numbers.js)

0 comments on commit 62c6bc1

Please sign in to comment.