vendredi 2 février 2018

Generating random numbers in a loop results in the same number [duplicate]

This question already has an answer here:

This works if I step through it, but when I run it, it will only give me a result such as : 4 4 4 4 4 4 4 4 4 4

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  

namespace ConsoleApplication1  
{  
class Program  

{  

    public static int diceRoller(int x, int y)  
    {  
        Random rnd = new Random();  
        int result = rnd.Next(x, y+1);  
        return result;  
    }  

    static void Main(string[] args)  
    {  

        int[] myArray = new int[10];  

        for (int i = 0; i < myArray.Length; i++)  
        {  
            myArray[i] = diceRoller(1, 6);  

            Console.WriteLine(myArray[i]);    
        }  

        Console.ReadKey();  

    }  
   }  
 } 




Aucun commentaire:

Enregistrer un commentaire