 # Distribution of Random Numbers in JavaScript

## The Issue with `Math.ceil(Math.random())` 🤔

In JavaScript, generating random numbers is a common task, but it’s crucial to understand the distribution of these numbers to ensure fairness and accuracy in applications. A frequently encountered solution is using `Math.ceil(Math.random())`, but this approach has its pitfalls.

### Why `Math.ceil(Math.random())` is Problematic 🚫

1. Bias Towards 1: `Math.random()` generates a number ranging from 0 (inclusive) to 1 (exclusive), typically very close to 0. When `Math.ceil()` is applied to this, the number is rounded up to the nearest integer. This process makes the occurrence of 1 slightly more likely than other numbers.

2. Possibility of Zero: Although rare, `Math.random()` can return 0. In this case, `Math.ceil(0)` will still yield 0. This behavior contradicts the expectation of generating a number between 1 and 100.

## A Better Approach ✅

To achieve a more evenly distributed range of random numbers, it’s advisable to use `Math.floor(Math.random() * 100) + 1`. This method ensures:

• Uniform Distribution: Each number from 1 to 100 has an equal chance of occurrence.
• No Zeroes: The range strictly stays between 1 and 100, as intended.

### Code Example 💻

``````function generateRandomNumber() {
return Math.floor(Math.random() * 100) + 1
}``````

This function will consistently give you a random number between 1 and 100 with a uniform distribution.

## Conclusion 📝

While `Math.ceil(Math.random())` might seem like a straightforward solution for random number generation, it introduces a slight bias and the possibility of zero, which might not be desirable. For a better distribution and reliability, `Math.floor(Math.random() * 100) + 1` is the way to go.

Understanding these nuances is key to effective programming in JavaScript, especially when dealing with randomness and its implications.