Advertisement
mireska

Untitled

Nov 20th, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getRandomIntInclusive(min, max) {
  2.     min = Math.ceil(min);
  3.     max = Math.floor(max);
  4.     return Math.floor(Math.random() * (max - min + 1)) + min; //Максимум и минимум включаются
  5. }
  6.  
  7. class Paradox {
  8.     constructor() {
  9.         this.doorWithCar = getRandomIntInclusive(0, 2)
  10.         this.playerChose = getRandomIntInclusive(0, 2)
  11.     }
  12.     openTheDoor() {
  13.         let openedDoor
  14.         while(openedDoor !== this.doorWithCar && openedDoor !== this.playerChose) {
  15.             openedDoor = getRandomIntInclusive(0, 2)
  16.         }
  17.         const result = this.result(openedDoor)
  18.         return result
  19.     }
  20.     result(openedDoor) {
  21.         let changedPick = this.changePick(openedDoor);
  22.         if(changedPick === this.doorWithCar) {
  23.             return 1
  24.         }
  25.         if(this.playerChose) {
  26.             return 0
  27.         }
  28.     }
  29.     changePick(openedDoor) {
  30.         let changedPick
  31.         while(changedPick !== openedDoor && changedPick !== this.playerChose) {
  32.             changedPick = getRandomIntInclusive(0, 2)
  33.         }
  34.         return changedPick
  35.     }
  36. }
  37. let playerWins = 0
  38. let changedWins = 0
  39. for(let i = 0; i < 100000; i++) {
  40.     const game = new Paradox()
  41.     const result = game.openTheDoor()
  42.     if(result === 0){
  43.         playerWins++
  44.     }
  45.     if(result === 1) {
  46.         changedWins++
  47.     }
  48. }
  49. console.log('Без изменения выбора: ' + playerWins,'С изменением выбора: ' + changedWins)
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement