سلام دوستان من یه آرایه دارم و میخوام تو خونه هاش اعداد تصادفی صحیح بین 0 تا 9 رو جای گذاری کنم...میخواستم بپرسم چطوری میتونم این کارو انجام بدم؟
کد زیر رو امتحان کردم ولی کل آرایه یک عدد تکراری میگیره....
for (int i = 0; i < 3; i++)
{
Random rnd = new Random();
int temp = 0;
temp = rnd.Next(0, 9);
page[i] = temp;
}
همچنین از تابع زیر استفاده کردم ولی ارور میده که ارورشو زیر بهتون میگم...
IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = minInclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
Random rnd = new Random();
while (candidates.Count > 1)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}
به این صورت استفاده کردم ازش :
for (int i = 0; i < 3; i++)
{
page[i] = UniqueRandom(0, 9);
}
ارور هم اینه :
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
نمیدونم چطوری باید به int تبدیلش کنم...
در ضمن اینم بگم که فضای نام :
using System.Collections.Generic;
هم اضافه کردم...مرسی