简体   繁体   中英

Need help assigning values to array C# (noob)

I am trying to write a method that will assign each day of the year a value for rainfall after checking if it rain at all.

So I want my days array to contain 365 random numbers below 28, 3/4 of them being 0.

note: I have a global random variable

static void Generate() 
{
    int[] days = new int[365];

    int going_to_rain = 0;
    for (int i = 0; i < days.Length; i++) 
    {
        going_to_rain = randomValue.Next(3);
        if (going_to_rain == 1) 
        {
            days[i] = randomValue.Next(1, 28);
        } 
        else 
        {
            days[i] = 0;
        }
    }
    Console.WriteLine(days);
}

You can create an array that its first 274 cells are 0, and the others are random.

Afterward you shuffle this array randomally:

int[] days = new int[365];
int i = 0;
for(i = 0;i < 274;++i)
{
   days[i] = 0;
}

for (i = 275;i < 365; ++i)
{
   days[i] = randomValue.Next(1,28);
}

//Shuffle
for (i = 0; i < 365; ++i)
{
   int randVal = randomValue.Next(364);
   int tmp = day[randVal];
   day[randVal] = day[i];
   day[i] = tmp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM