Distributing a quantity r in m boxes
Feb 24, 2014 at 12:06am UTC
I will make a mess of explaining this, but I'm stuck..
I have an array of size
m and
r resources. What would be an efficient (or just any) way of distributing them in the way shown in the table? The highest amount of resources must be concentrated in the middle of the array and all the resources must be distributed.
I'm not really set on exactly how steep the "slope" must be, I just need a way of approaching the problem.
Example with
m=9 and
r=13
1 2 3 4 5 6 7 8 9 10
box resources
0 0
1 0
2 2
3 2
4 4
5 2
6 2
7 1
8 0
Thanks :)
Last edited on Feb 24, 2014 at 12:10am UTC
Feb 24, 2014 at 2:20am UTC
There are so many ways ... You need to provide more information. One ideea is to put all resources in the box m/2 or the closest int.
Feb 24, 2014 at 6:54am UTC
using std::random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <cstring>
#include <random>
using namespace std;
random_device rd;
mt19937 gen(rd());
int *box(const int size, const int res) {
int *array = new int [size];
memset(array, 0, sizeof (int ) * size);
const int mid = size >> 1;
uniform_int_distribution<int > dist(0, size);
for (int t = 0; t< res; ++t) {
int index = dist(gen);
if (index > mid) index--;
else if (index < mid) index++;
array[index % size]++;
}
return array;
}
int main() {
int *out = box(9, 13);
for (int s = 0; s < 9; ++s)
cout << out[s] << endl;
return 0;
}
Or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <cstring>
#include <random>
#include <cmath>
using namespace std;
random_device rd;
mt19937 gen(rd());
int *box(const int size, const int res) {
int *array = new int [size];
memset(array, 0, sizeof (int ) * size);
const int mid = size >> 1;
normal_distribution<> dist(mid, 1.0);
for (int t = 0; t< res; ++t) {
int index = dist(gen);
array[int (round(index))]++;
}
return array;
}
int main() {
int *out = box(9, 13);
for (int s = 0; s < 9; ++s)
cout << out[s] << endl;
return 0;
}
Last edited on Feb 24, 2014 at 7:43pm UTC
Feb 24, 2014 at 2:46pm UTC
@ats15
Yes you're right, I didn't realize that even putting all the resources in m/2 would satisfy my requirements.
@Smac89
I hadn't thought of using a distribution for the index. Very cool. Thanks!
Topic archived. No new replies allowed.