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));
Your explanation is quite confused. However if you check the operators' precedence you will find that
return(rand() % 1-f);
is equivalent toreturn((rand() % 1) - f);
. Is this really what you want? After allrand() % 1
will always result in zero. Another important point is that although you have encoded a loopfor
the function will be closed in the firstreturn
.– anonimo
@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!
– Yasmin