Generate a list of characters based on pre-defined values

Hi,

Hoping someone can help me out.
I am trying to create a basic little program that will generate a list of values from a pre-defined list of acceptable characters.

These characters will be - 'H', '/', 'L'.

The program should print these out in a random order with the majority of them being '/' with about 5% being 'L' and 5% being 'H'. (The percentages can vary).
And the program should ask or have a variable that can determine how many characters it will print (i.e. 5 or 5000).

An example of the print out Im looking for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/
/
/
/
/
L
/
/
/
/
/
H
H
/
/
/
/

Etc.

What would the best way be around this? Im really stumped and have no idea where to begin.
Obviously I would create a list of the said characters.
But how do I make the program favour one over the other based on percentages?

This is part of work that Im doing at my job where I have to generate a set of dummy data in various stages so this would save me hours of work if I can figure it out.

Any guidance or pointers would be appreciated.

Thank you
Try std::discrete_distribution in the <random> library.

You can generate numbers 0, 1, 2 with given weights and use these as the indices of a string or char array.
@lastchance thanks for the pointer!
I ended up going slightly differently.
Heres the final code in case it is useful to anyone else in the future:

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 <cstdlib>
#include <fstream>
using namespace std;
ofstream myfile;

void GenerateRandom() {
    int randomNumber;
    for (int index = 0; index < 100; index++) {
        randomNumber = (rand() % 100) + 1;


        if (randomNumber <= 2) {
            myfile << "H" << endl;
        }
        if (randomNumber > 2 && randomNumber <= 6) {
            myfile << "L" << endl;
        }
        else {
            myfile << "/" << endl;
        }
    }

};

int main()
{
    myfile.open("C:\\Users\\Miuskaste\\Desktop\\Test.txt");
    GenerateRandom();
    myfile.close();
}


Thanks again!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <random>
#include <string>
#include <ctime>
using namespace std;

mt19937 gen( time( 0 ) );
discrete_distribution<int> dist{ 5, 5, 90 };

int main()
{
   const string opt{ "HL/" };
   const int N = 100;
   for ( int i = 0; i < N; i++ ) cout << opt[dist(gen)];
}
wirelesskill, did you mean for your second if to not be an else-if?
Topic archived. No new replies allowed.