Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Задание:
- Через канал с шумами передается последовательность битов.
- Если бит передан корректно, то следующий бит передан корректно с
- вероятностью 0.8.
- Если бит передан некорректно, то следующий бит передан некорректно с
- вероятностью 0.7.
- Определить процент некорректных битов. Т.е. до конца занятия студент
- должен сообщить свою фамилию и 1 число. Решать можно как угодно,
- аналитически или используя имитационное моделирование.
- Ответ: ~40% */
- //Mayatsky Alexander, 2010
- #include <iostream>
- #include <ctime>
- #define LOOP_ITERATIONS 1000000
- /* Nextbit determination function */
- bool nextbit( bool previousbit ){
- if ( previousbit ){
- if ( std::rand() % 101 <= 80 ){
- return true;
- }
- else return false;
- }
- if ( ! previousbit ){
- if ( std::rand() % 101 <= 70 ){
- return false;
- }
- else return true;
- }
- return false; //This return placed here to supress warnings only.
- }
- int main( int argc, char *argv[] ){
- /* Random seed initialising, using unixtime */
- time_t ltime;
- time ( <ime );
- srand( (unsigned int)ltime );
- /* Main Loop */
- int correctbits = 0;
- int failbits = 0;
- bool currentbit = false;
- for ( int i = 0; i < LOOP_ITERATIONS; i++ ){
- currentbit = nextbit(currentbit);
- if ( currentbit ){
- correctbits++;
- }
- else {
- failbits++;
- }
- }
- /* Results output */
- std::cout << (float)correctbits / LOOP_ITERATIONS * 100 << '%' << " of correct bits\n";
- std::cout << (float)failbits / LOOP_ITERATIONS * 100 << '%' << " of fail bits\n";
- std::getchar();
- }
- /* Typical output:
- 60.0482% of correct bits
- 39.9518% of fail bits
- 59.8607% of correct bits
- 40.1393% of fail bits
- 59.9462% of correct bits
- 40.0538% of fail bits
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement