question

CSN avatar image
0 Likes"
CSN asked Jason Lightfoot edited

Generate 100 random numbers

How to generate 100 random numbers, sum to 1000 and I can set the dispersion of these 100 data, and run in the experimenter or optimizer?

FlexSim 22.0.16
random numbers
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Felix Möhlmann avatar image
1 Like"
Felix Möhlmann answered Jason Lightfoot edited

Generate 1000 values with a statistical distribution of your choice with a mean value of 10, saving them in an array or table.

Sum the numbers up to get the offset to your target sum of 1000. Adjust each number proportionally by that offset (for example decrease each value by 0.2% if your sum is 1002).

Create a parameter that controls which of the values will be accessed during a simulation run. So the parameter would either represent the index of the array or the row of the table.

· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jason Lightfoot avatar image Jason Lightfoot ♦♦ commented ·

For integers you could use something like this:

Array integers=Array(100);
for (int n=1000;n>0;n--)
    integers[duniform(1,100)]++;  // (add a stream for repeatability)
return integers;
0 Likes 0 ·
CSN avatar image CSN Jason Lightfoot ♦♦ commented ·

Thank you, how to set the dispersion of these 100 data

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ CSN commented ·

You could affect the value spread by altering the index generation ( a custom distribution rather than uniform). Then you can randomly resequence the array if needed.

int numValues=100;
Array integers=Array(numValues);
for (int n=1000;n>0;n--){
    int idx=Math.min(Math.max(1,normal(numValues/2,numValues/5)),numValues);
    integers[idx]++;  
}
Array result; 
while (integers.length)
     result.splice(duniform(1,result.length),0,integers.pop());
return result;


0 Likes 0 ·
CSN avatar image CSN commented ·

Can you describe in detail how to implement this

0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann CSN commented ·

Here's a simple code that generates 100 values from a normal distribution that sum to 1000 and writes them to a column in a global table.

Table destTable = Table("GlobalTable1");
int col = 1; int numValues = 100; double targetSum = 1000; double mean = targetSum/numValues; double stdDevFactor = 0.2; double stdDev = mean*stdDevFactor; destTable.setSize(Math.max(numValues, destTable.numRows), Math.max(col, destTable.numCols)); Array values = []; double sum = 0; for(int i = 1; i <= numValues; i++) {     double value = normal(mean, stdDev, realtime(1));     sum += value;     values.push(value); } double offsetFactor = sum/targetSum; for(int i = 1; i <= values.length; i++) {     destTable[i][col] = values[i]/offsetFactor; }

Since I don't know how you plan to use those numbers, I can't provide any inside into how to proceed from there.

1 Like 1 ·
CSN avatar image CSN Felix Möhlmann commented ·

Thank you for your reply. It helps a lot.

0 Likes 0 ·