percentage of values

if i wanted to give a set of values each a percentage of being chosen how would i go about that for example if i wanted the program to pick a number from one to ten and wanted it to choose all numbers 10% with the exception of 10 being chosen 11% and 1 being chosen 9% of the time
Last edited on
well lol dont think i am quite there yet thats a little over my head, but thanks for your help. I was hoping i could just assign each value a percentage
oh wow this just clicked for me thank you
What can you do:
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
#include <iostream>
#include <random>

int main()
{
    const int nrolls = 1000000; // number of experiments

    std::default_random_engine generator;       //↓ We don't want any zeroes
    std::discrete_distribution<int> distribution {0, 9,10,10,10,10,
                                                    10,10,10,10,11};
    //actually you just could add 1 to every random roll, but lets leave it like that for now

    int p[11]= {}; //An array holding results of experiment

    //Generating random number nrolls times
    for (int i=0; i<nrolls; ++i) {
        int number = distribution(generator);
        ++p[number]; 
    }
    //Outputting our distribution.
    std::cout << "a discrete_distribution:" << std::endl;
    for (int i=0; i<11; ++i)
        std::cout << i << ": " << (double)p[i]/nrolls*100 << "%"<< std::endl;
        
    return 0;
}

Output:
a discrete_distribution:
0: 0%
1: 8.98%
2: 9.9638%
3: 9.995%
4: 9.9555%
5: 10.0493%
6: 10.0212%
7: 10.0052%
8: 10.0389%
9: 9.9725%
10: 11.0186%
Last edited on
Topic archived. No new replies allowed.