Can the c++ function be generated as a probability?

Asked 2 years ago, Updated 2 years ago, 25 views

For example,

A(), B(), C() There is a function like this.

A() function occurs with a 10% probability B() Samsu occurs at 20% and C() at 30%.

Is there a way to squeeze it like this?

c++

2022-09-22 19:12

3 Answers

Create an algorithm that returns a specific value probabilistically using random functions, etc.
You can look at that value and then you can code it to decide which function to call.

The bottom part is just an example There will be many better ways. Since the random function doesn't really spit out the value randomly, You're going to have to add a lot more ideas to calculate the probability more accurately.

int ary[10] = {1, 2, 2, 3, 3, 3, 0, 0, 0, 0};
int index = 0;
int nfunc = 0;

index = random() %10; // Random values from 0 to 9
nfunc = aary[index]; // Gets the value of the array with a random value

if (nfunc == 1)  A();
else if (nfunc == 2) B();
else if (nfunc == 3) C();


2022-09-22 19:12

Implementing random functions is very difficult. Thus, in c++, a random number function is implemented in <cstdlib>.


#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    for(int i=0; i<10; i++)
        cout<<rand()<<endl;
}

But if you run this code, you can see that the same values always come out in order, and that's why seed. The seed of a random function must be specified, but it is always the default because it is not specified. So programmers use the following code:


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){
    srand(time(0));
    for(int i=0; i<10; i++)
        cout<<rand()<<endl;
}

It's using the time function as seed. Because time changes all the time, you can always get a different random numbers.

Now, if you apply it, you'll see:


#include <bits/stdc++.h>
using namespace std;

void A(){
    if(rand()%10>0)
        return;
    //code
}

void B(){
    if(rand()%10>1)
        return;
    //code
}

void C(){
    if(rand()%10>2)
        return;
    //code
}

int main(){
    srand(time(0));
    //code
}


2022-09-22 19:12

If the function has the same factor and return value, you can use the function pointer to do something interesting.

void A() { cout << "A" << endl; }
void B() { cout << "B" << endl; }
void C() { cout << "C" << endl; }
void E() {} // do nothing

You can write the following code under the assumption that: At this time, the return value of A cannot necessarily be void, so let's do the following.

// declotype(A): Use the type of function A by inferring it
// Decltype(A) * : Function pointer of function A
decltype(A) * arr[10] {  A, B, B, C, C, C, E, E, E, E };

Random numbers don't matter what you use, and you can mix arr directly. In the example of this answer, let's use the standard library #include <random>.

#include <iostream>
#include <random>

using namespace std;

void A() { cout << "A" << endl; }
void B() { cout << "B" << endl; }
void C() { cout << "C" << endl; }
void E() {} // do nothing

int main()
{
    // Decltype(A): Use the type of function A by inferring the type of function A
    // Decltype(A) * : Function pointer of function A
    decltype(A) * arr[10] {  A, B, B, C, C, C, E, E, E, E };

    random_device dre;
    uniform_int_distribution<> ui{ 0, 9 };

    // // random index
    auto random_index = ui(dre);

    // // call func
    arr[random_index]();
}


2022-09-22 19:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.