Hello,
I need to randomly generate a number between 10000 and 25000, then randomly distribute the result into 10 different variables.
Anyone have any ideas on how to do this in .js?
Thanks for any replies
Maybe I wasn't very clear on what I'm trying to do. Here's (hopefully) a better way to describe the operation that I'm trying to achieve
Generate a random number between 10000 and 25000 (done- let's say it's 15000)
Then; take the result (15000) and distribute it over 10 variables by random percentage
Resulting in
var a= 1500 (10% of total)
var b= 750 (5% of total)
etc. until 100% of total is reached
Random.Range(10000,25000) will give you a float between the two, and you can use MathF.Round() to get an int from there.
As for distributing the results: you could either call Random.Range() 10 times, and assign it to a different variable each time, or you could use a loop, and assign it to a different index in an array of variables...Lots of ways you could go:
var Arr : Array = new Array();
for (var i = 0; i < 10; i++) {
Arr.Add(MathF.Round(Random.Range(10000,25000)));
}
Alternatively:
var Arr : int[] = new int[10];
for (var i = 0; i < 10; i++) {
Arr *= MathF.Round(Random.Range(10000,25000));*
*}*
*```*
*<p>Or the most basic way:</p>*
*```*
*var A = MathF.Round(Random.Range(10000,25000));*
*var B = MathF.Round(Random.Range(10000,25000));*
*// etc*
*var J = MathF.Round(Random.Range(10000,25000));*
*```*
To find the answer to this sort of problem, it helps to think of a real-world model... In this case, a bag of marbles (10000 - 250000 of them) and 10 slots into which they randomly fall... If they fall one by one then your 10 slots are going to be filled rather evenly. In all likelihood you will have percentages like 10.001% To avoid this you can dictate that the balls sometimes or always fall in groups. By adjusting the minimum and maximum size of the groups you adjust the deviation (a high deviation will have slots with 20% while others might have 4%).
In C#
int numSlots = 10;
int[] marbles = new int[Random.Range(10000,25000)];
int[] slots = new int[numSlots];
float[] percentages = new float[numSlots];
for (int i=0; i < numSlots; i++ ) {
slots *= 0;*
*}*
*int i=0;*
*int minGroupSize = 1;*
*int maxGroupSize = 100;*
*while (i < marbles.Length) {*
*int marbleGroup = Math.Clamp(Random.Range(minGroupSize, maxGroupSize), 1, marbles.Length - i);*
*slot[Random.Range(0,9)]+=marbleGroup;*
*i += marbleGroup;*
*}*
*for (int i=0; i < numSlots; i++) {*
<em>percentage _= slot */ marbles.Length;*_</em>
<em>_*}*_</em>
<em>_*```*_</em>