How to generate a Boolean (0|1) randomly given the probability of the truth event in C?

Asked

Viewed 135 times

-3

I am trying to create a function for a larger problem, and only the function has the following description: returns a non-null integer with probability f, and returns 0 with probability 1 f, where 0 f 1

My code is at this point, but I think I’ve mixed concepts and I don’t even know what I’m doing anymore. How to fix?

int sorteia_voto_com_falha (double f);
    int i, N;

    srand(RAND_MAX);
    for(i = 1; i < N; i++)
       return(rand() % 1-f);

In main() the person enters the number of votes N, and the statement basically says that each vote that a person receives, has probability f to be accounted for with failure for the opposite candidate.

Link to the statement

  • Your explanation is quite confused. However if you check the operators' precedence you will find that return(rand() % 1-f); is equivalent to return((rand() % 1) - f);. Is this really what you want? After all rand() % 1 will always result in zero. Another important point is that although you have encoded a loop for the function will be closed in the first return.

  • @Thank you very much for your return. I included a link that shows the problem statement, maybe it’s clearer what I’m trying to do, than I explain in my words. Please consider a reading. I am desperately trying to learn, to go slowly to understand the general, unfortunately I have no one to ask. Thank you!

1 answer

0

The function rand() returns a value between 0 and RAND_MAX.

This variable is set in the file stdlib.h.

And they’re outfitted in a break:

_____________________________________
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
-------------------------------------
0    ^      RAND_MAX/2           RAND_MAX
     |
     5% estão abaixo desse número

Its function HouveFalha(double probabilidade) must be something like:

int HouveFalha(double probabilidade)
{
  return ( ( (double)rand() / (double)RAND_MAX ) < probabilidade ) ? 1 : 0;
}

Don’t forget to initialize a seed with srand():

srand(time(NULL));

link to IME

Ternary operator

The operator ? used in the code is a ternary operator, it is called ternary because it receives three operands.

In functional language would be the equivalent:

?(condition, truth, false)

In Agaric notation (the one used in programming language) it is used as follows:

condition ? truth : false

that line is equivalent to:

if(condition) { Truth Return; } Else { fake Return }

From writing so much this code someone invented this shortcut.

In that case the condição is:

( ( (double)rand() / (double)RAND_MAX ) < probabilidade )

The seed of the GNA Random Number Generator (RNG)

Rand() generates pseudo-random numbers, i.e.:

  • They are generated mathematically with some account

  • The seed is used to initialize the algorithm.

  • Can be equipped in an interval.

  • Given the same seed, the same sequence will be generated.

  • Not suitable for encryption/security.

  • Useful in everyday Monte Carlo simulations.

To create a randomness in the seed, the current time is used as seed, it is not mandatory, but it is a common practice:

srand(time(NULL));
  • As it’s getting longer and slipping away from the focus of the answer, your conversation was moved to the chat, which is more suitable for interactions of this type. You can proceed normally by clicking on the given link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.