0

Hi i writing this below code for make some person. in query i want to get all persons and select instance form every person this below code is for Main method:

Persons[] person = { new Persons(534, "Komeil", "Shahmoradi", 20, 1.65f, 68.3f), 
                           new Persons(1047, "Ahmad", "Darvishi", 18, 1.72f, 70) ,
                            new Persons(56, "Javad", "Amini", 28, 1.56f, 73.2f),
                            new Persons(2, "Hossein", "Kiany", 17, 1.80f, 65.6f) ,
                           new Persons(192, "Hossein", "Kazemy", 15, 1.80f, 83.6f),
                           new Persons(2002, "Hossein", "Saeedi", 43, 1.80f, 93.6f)};

Random rnd = new Random();
var QpersonsLocation = from value in person
                  select new Duty(rnd.Next(1,2000), value.pName, value.pFamily, value.pAge);


Console.WriteLine("[/] System seting location");
foreach (var item in QpersonsLocation)
{
   Console.WriteLine(item.dId +"\t" + item.dName + "\t" + item.dFamily + "\t" + item.dAge + "\t" + item.dDuttyLocation+"\t");
}

And this is for my Persons class:

class Persons
{
    public long pId { get; set; }
    public string pName { get; set; }
    public string pFamily { get; set; }
    public byte pAge { get; set; }
    public float pSize { get; set; }
    public float pWeight { get; set; }

    public Persons(long pId, string pName, string pFamily, byte pAge, float pSize, float pWeight)
    {
        this.pId = pId;
        this.pName = pName;
        this.pFamily = pFamily;
        this.pAge = pAge;
        this.pSize = pSize;
        this.pWeight = pWeight;
    }
}

problem in the lass code for Duty class :

class Duty
{

    string[] locations = {"Esfahan",
        "Tehran",
        "Mashhad",
        "Shiraz",
        "Hamedan",
        "Azarbayejan"};
    public long dId {get;set;}
    public string dName { get; set; }
    public string dFamily { get; set; }
    public byte dAge { get; set; }

    public string dDuttyLocation { get; set; }

    public Duty(long dId, string dName, string dFamily, byte dAge)
    {
        this.dId = dId;
        this.dName = dName;
        this.dFamily = dFamily;
        this.dAge = dAge;
        Random rnd = new Random();
        int num = rnd.Next(0,locations.Length);
        dDuttyLocation = locations[num];//problem            
    }


}

The code working correctly but the location not saving and always dDuttyLocation equals the last number of random number this below picture shows the result: result of program

1 Answers1

0

The problem is creating new instances of the Random class too close in time in your Duty constructor. See here for more info.

Similar to how you're already doing it to generate a new dId for each person, you would need to generate the random number and pass it into the constructor so that they are not all the same.

To do that, you'd first need to make your locations array public and static. Then you can change your LINQ expression to something like:

Random rnd = new Random();
var QpersonsLocation = from value in person
                       select new Duty(rnd.Next(1,2000), value.pName, value.pFamily, value.pAge)
                       {
                           dDuttyLocation = rnd.Next(Duty.locations.Length)
                       };
Connor
  • 807
  • 1
  • 10
  • 20