DrDerp20 - 2 months ago 6

C# Question

I'm making a system to randomly generate stars based on type. The first part is of course telling the system what type of star it is based on rarity. I am having difficulty shortening the actual assigning part based on a random number generator. Here is my current solution.

`void StarTypeGen () {`

randomGen (x: 0, y: 229000);

if (Rng <= 228000) {

StarType++;

if (Rng <= 36000) {

StarType++;

if (Rng <= 22500) {

StarType++;

if (Rng <= 9000) {

StarType++;

if (Rng <= 3750) {

StarType++;

if (Rng <= 1875) {

StarType++;

if (Rng <= 10) {

StarType++;

}

}

}

}

}

}

}

}

Answer

Preload an array with the values and loop through breaking on the first false condition:

```
int[] nums = new int[] { 228000, 36000, 22500, 9000, 3750, 1875, 10 };
for (int i = 0; i < nums.Length; i++)
{
if (Rng > nums[i]) break;
StarType++;
}
```

Source (Stackoverflow)

Comments