## How can I generate random variables according to data types?

continuous random variable
types of random variable
random variable examples
discrete random variables
continuous random variable example
what is meant by random variable
random variable formula
discrete random variable example

I have some databases and tables. I am trying to detect data types of each column and create random variables according to data type?

For example:

```col1 : int
col2 : nvarchar(max)
col3 : bit
```

I need to detect each of the data types in code and create random variables for insert.

Should I create a methods, which check all data types and generate variables, for this work? like:

```if(datatype.Equals("int"))
random.Next();
else if(datatype.Equals("string"))
...
```

Is there any easy way?

Here's an approach:

```var random = new Random();
var factory = new Dictionary<Type, Func<object>>()
{
{ typeof(int), () => random.Next() },
{ typeof(string), () =>
{
var bytes = new byte[16];
random.NextBytes(bytes);
return Convert.ToBase64String(bytes);
}
},
{ typeof(DateTime), () => DateTime.Now.AddDays((random.NextDouble() - 0.5) * 100) },
};
```

Then you can do this:

```var datatype = typeof(string);
var output = factory[datatype]();
Console.WriteLine(output);
```

I get `eyqQ1EdMDTsR8Ny8kS73Hg==` as an example output.

You just need to keep building up all of the types you need.

Random Variables, There are two types of random variables, discrete and continuous. The probability distribution of a discrete random variable is a list of A random number generator acting over an interval of numbers (a,b) has a continuous distribution. Discrete Random Variables. Discrete random variables can take on either a finite or at most a countably infinite set of discrete values (for example, the integers). Their probability distribution is given by a probability mass function which directly maps each value of the random variable to a probability.

There is no "SQL Server data type -> .NET data type" mapping table built-in; thus, you'll have to do the mapping yourself.

You can check SQL Server to .Net type conversions

Six Fundamental Methods to Generate a Random Variable – Win , Good references for the modern theory include: If we assume we can generate a random variable according to the distribution p(x) we can� Each of these types of variable can be broken down into further types. Quantitative variables. When you collect quantitative data, the numbers you record represent real amounts that can be added, subtracted, divided, etc. There are two types of quantitative variables: discrete and continuous.

The Random class provides `Random.Next()`, `Random.NextBytes()`, and `Random.NextDouble()` methods. The Random.Next() method returns a random number, `Random.NextBytes()` returns an array of bytes filled with random numbers, and `Random.NextDouble()` returns a random number between 0.0 and 1.0.

The Random.Next() method has three overloaded forms and allows you to set the minimum and maximum range of the random number.

The following code returns a random number.

```int num = random.Next();
```

The following code returns a random number less than 1000.

```int num = random.Next(1000);
```

The following code returns a random number between the min and the max range. To Generate a random number between two numbers

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

You can even combine the two methods - RandomNumber and RandomString to generate a combination of random string and numbers.

```// Generate a random string with a given size
public string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random 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();
}
```

Reference

Use the `GetType()` method to know the data type in C#. You can learn about it Here