---
title: "How to generate random numbers in C#?"  
description: "How to generate random numbers in C#?"  
author: "Revati S Misra"  
published: 2023-08-18  
updated: 2023-08-19  
canonical: https://www.mindstick.com/forum/159580/how-to-generate-random-numbers-in-c-sharp  
category: "c#"  
tags: ["c#", "random number"]  
reading_time: 2 minutes  

---

# How to generate random numbers in C#?

How to [generate random](https://www.mindstick.com/forum/159231/what-is-the-way-to-generate-random-alpha-numeric-string-in-java) numbers in C#?

## Replies

### Reply by Aryan Kumar

To generate [random numbers](https://www.mindstick.com/forum/832/generate-random-numbers-uniformly-over-entire-range) in C#, you can use the `Random` class. The `Random` class is a static class, which means that you do not need to create an instance of it. You can simply call its methods directly.

The `Random` class has a number of methods for generating random numbers. The most commonly used method is the `Next()` method. The `Next()` method takes two parameters: the minimum value and the maximum value. The `Next()` method will return a random number between the minimum value and the maximum value, inclusive.

For example, the following code will generate a random number between 1 and 100:

C#

```plaintext
int randomNumber = Random.Next(1, 100);
```

You can also use the `NextDouble()` method to generate a random number between 0 and 1.

For example, the following code will generate a random number between 0 and 1:

C#

```plaintext
double randomNumber = Random.NextDouble();
```

The `Random` class also has a number of other methods for generating random numbers. You can find more information about the `Random` class in the C# documentation.

Here are some additional things to keep in mind when generating random numbers in C#:

- The `Random` class uses a seed value to generate random numbers. The seed value is an integer that is used to initialize the random number generator. The same seed value will always generate the same sequence of random numbers.
- You can change the seed value by calling the `SetSeed()` method. The `SetSeed()` method takes an integer as its parameter.
- The `Random` class is not thread-safe. This means that you cannot use it to generate random numbers from multiple threads at the same time. If you need to generate random numbers from multiple threads, you should use a thread-safe random number generator.


---

Original Source: https://www.mindstick.com/forum/159580/how-to-generate-random-numbers-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
