producing random numbers within a range

ou can try

``````//for integers
Random r = new Random();
int rInt = r.Next(0, 100);

//for doubles
int range = 100;
double rDouble = r.NextDouble()* range;``````

You can Try below code also

``````Random rnd = new Random();
int month = rnd.Next(1, 11); // creates a number between 1 and 10
int dice = rnd.Next(1, ````8````); // creates a number between 1 and 7
int card = rnd.Next(51); // creates a number between 0 and 50``````

Use:

``````Random r = new Random();
int x= r.Next(10);//Max range``````

`new Random()` it is initialized. This means that in a tight loop you get the same value lots of times. You should keep a single `Random` instance and keep using `Next` on the same instance.

``````//Function to get random number
private static readonly Random getrandom = new Random();

public static int GetRandomNumber(int min, int max)
{
lock(getrandom) // synchronize
{
return getrandom.Next(min, max);
}
}``````

methods that generate random numbers which help for various purposes. I hope this may help you too:

``````public class RandomGenerator
{
public int RandomNumber(int min, int max)
{
var random = new Random();
return random.Next(min, max);
}

public string RandomString(int size, bool lowerCase)
{
var builder = new StringBuilder();
var random  = new Random();
char ch;

for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}

if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
}``````