-1

I am making a space game with planets that are randomly generated and I need their atmospheres to be made up of random amounts of different elements. So the thing is that I want the elements to be percentages of the complete atmosphere, an example: oxygen = 30, nitrogen = 20, carbondioxide = 50, hydrogen = 0. All these values should be completely randomized and the sum of them all has to be 100.

I basically want to fill a container to the top with random amounts of set elements, but I don't know how to randomize all of the variables and end up with a fixed sum.

This is my first time submitting anything to StackOverflow so please let me know if there is anything I need to clarify, I've been stuck on this issue for so long without finding any answers so I would appreciate any help, thanks :)

(I am using c# in unity in case that makes a difference)

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
SP3sheep
  • 1
  • 1

2 Answers2

2

Well, you could think of it from the other perspective, and generate random values for each element, then use their total as 100%.

For instance...

var rnd = New Random()
ChecmicalsInAir = 9;
Hydrogen = rnd.Next();
Oxygen = rnd.Next();
...
TotalAtmos = (Hydrogen + Oxygen + ...);

Yes - as pointed out by canton7, below:

Dividing the chemical by TotalAtmos will give you the percentage of atmosphere.

Just to add a little more to this answer, you may also wish to use a Dictionary to store the information, rather than simple variables; that way you can include different gases in the atmosphere, as not all planets may have oxygen or nitrogen, this could add different bonuses or penalties, but remains a straightforward process for calculating percentages.

Paul
  • 4,160
  • 3
  • 30
  • 56
  • 1
    In case it wasn't clear: you can get values between 0 and 100 for each of your elements by dividing `Hydrogen`, `Oxygen`, etc, by `TotalAtoms` and then `* 100` – canton7 Dec 22 '20 at 16:50
  • 1
    @canton7 - well pointed out. I've updated my post, thank-you. – Paul Dec 22 '20 at 16:52
0

Questions like "how would you design this..." are better suited for Game Development. Those questions tend to get downvoted here a lot.

Here's one way you could do it.

int elementCount = 3;    // set to the number of elements you have
List<float> elementPercents = new List<float>();

// Assign an initial value between 0 and 1 to each element, and set totalWeight
// to the sum of all those values.  Note that this lets you have 0 for an element.
// If you don't want 0 for an element, adjust the Random.Range
float totalWeight = 0;
for (int i = 0; i < elementCount; i++)
{
   elementPercents.Add(UnityEngine.Random.Range(0f, 1f));
   totalWeight += elementPercents[i];
}

// Set the percent of each element to its proportion of the total weight.
// For example, if Oxygen = 0.1 and Hydrogen = 0.2 and Nitrogen = 0.3 from the above statement,
// then Oxygen's percent = 0.1 / (0.1 + 0.2 + 0.3) = 0.167
for (int i = 0; i < elementCount; i++) 
{
   elementPercents[i] /= totalWeight;
}

// Assign the percents to each element according to your requirements, for example
float hydrogen = elementPercents[0];
float nitrogen = elementPercents[1];
float oxygen = elementPercents[2];
Ben Rubin
  • 6,909
  • 7
  • 35
  • 82