I have a class that name Cleint in that i have a function ,i want to make a three instantiate from Cleint with diffrent result of a Function but the results are same here is my code:
Programm.cs:
class Program
{
static void Main(string[] args)
{
Cleint B = new Cleint();
Cleint k = new Cleint();
Cleint S = new Cleint();
Console.WriteLine(B.GenerateAddress());
Console.WriteLine(k.GenerateAddress());
Console.WriteLine(S.GenerateAddress());
Console.ReadLine();
}
}
and Cleint.cs:
class Cleint
{
public string GenerateAddress()
{
var parts = new List<string>();
Random random = new Random();
for (int i = 0; i < 4; i++)
{
int newPart = random.Next(0, 255);
parts.Add(newPart.ToString());
}
string address = string.Join(".", parts);
p;
return address;
}
}
thank’s for your help
Randomis not really random – it is pseudo random and the default constructor uses the current time as the seed for the sequence.When called in quick succession, this time will be the same and the sequence be the same.
You can use a static field to ensure you are using the same
Randominstance if you are only ever going to have one thread (asRandomis not thread safe):