C++ lec 30 linear Congruence Generator 2

preview_player
Показать описание
In LCG 1, we have run the program which gives the following output:

Values mod 2: 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
Values mod 4: 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0 3 2 1 0

Clearly, the sequence of values we produce in this manner is far from random!
However, we can change the way we extract zeros and ones from lcg. Consider the
following alternative main.

int main() {
cout "Values mod 2: ";
for (int k=0; k less than 20; k++) {
double x = lcg() / 64.;
cout less than less than int(2*x) " ";
}
cout less than less than endl;
return 0;
}

Here we first produce a double value x in the range [0,1) by dividing the output of
lcg by 64. We then multiply x by 2 (so we are now somewhere in [0,2)) and then
cast to type int (so the value is now either 0 or 1). Here’s the result.

Values mod 2: 0 1 0 1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 0 0
As you can see, the output appears, at least superficially, more random.

#C++, #CPP, #include, #cprogramming, #cpp, #cppprogramming,
#programminglife, #cppprogramminglanguage, #please_subscribe_my_channel,
#programming , #program, #programmer, #progamer, #programmers,
#codmobile, #code, #codes, #coding, #codclips
Рекомендации по теме