dimanche 22 janvier 2017

c# random int arrays [duplicate]

This question already has an answer here:

I want to create random int arrays for each of 4 bots (robots that move around a checkerboard.) Here's the code:

private void InitBots()                                // first time only
    {
        for (int i = 0; i <= numbots; i++)
        {
            bot r = new bot();
            r.seq = i;
            r.score = 0;
            GenerateRandomArray(0, 6, 243).CopyTo(r.strategy, 0);
            botList.Add(r);
        }
    }

private int[] GenerateRandomArray(int low, int high, int cnt)     
    {
        Random r = new Random();
        int[] result = new int[cnt];
        int[] tally = new int[high + 1];
        int sum = 0;
        int rn;
        for (int i = 0; i < cnt; i++)
        {
            rn = r.Next(low, high + 1);
            result[i] = rn;
            tally[rn]++;
        }

        int l = tally.Length;
        for (int j = 0; j < l; j++)
            sum = sum + tally[j];
        int avg = sum / l;
        return result;
     }

If I put a debug break on the return line line I get unique arrays for each bot. If I don't put a break the bots all have identical arrays. Don't know where to go from here since debugging seems to change the results.




Aucun commentaire:

Enregistrer un commentaire