AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

Generate random number in c#

By pushpam abhishek
Listen to this article

how to generate random number in c#


how to generate random number in c#

In this topic, we will see generate random numbers in c# with example. It provides Random.Next(), Random.NextBytes(), and Random.NextDouble() methods in Random number class. The Random.Next() method returns a random number, Random.NextBytes() returns an array of bytes filled and returns a random number between 0.0 and 1.0.

int num = random.Next(); 

The following code returns a random number less than 1000.

int num = random.Next(1000);  

The following code generates a password of length 10 with the first 4 letters lowercase, next 4 letters numbers, and last 2 letters as uppercase.

// Generate a random password  
public string RandomPassword()  
{  
    StringBuilder builder = new StringBuilder();  
    builder.Append(RandomString(4, true));  
    builder.Append(RandomNumber(1000, 9999));  
    builder.Append(RandomString(2, false));  
    return builder.ToString();  
}  

                               Share this article with your friends

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments