Advertisement
Guest User

cw06z03

a guest
Oct 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. //cw06z03
  2. //napisz program ktory utworzy zmienna typu byte gdzie kazdy bit bedzie losowany
  3. //jezeli wynikiem losowania bedzie 0,5 i wiecej wstaw 1, jezeli mniej to 0
  4. public class cw06z03
  5.     {
  6.         public static void main ( String[] args)
  7.         {
  8.             byte zmByte = 0;
  9.             double los1 = Math.random();
  10.             double los2 = Math.random();
  11.             double los3 = Math.random();
  12.             double los4 = Math.random();
  13.             double los5 = Math.random();
  14.             double los6 = Math.random();
  15.             double los7 = Math.random();
  16.             double los8 = Math.random();
  17.            
  18.             if (los1 > 0.5)
  19.                 zmByte = (byte) (zmByte | 1<<0);
  20.             if (los2 > 0.5)
  21.                 zmByte = (byte) (zmByte | 1<<1);
  22.             if (los3 > 0.5)
  23.                 zmByte = (byte) (zmByte | 1<<2);
  24.             if (los4 > 0.5)
  25.                 zmByte = (byte) (zmByte | 1<<3);
  26.             if (los5 > 0.5)
  27.                 zmByte = (byte) (zmByte | 1<<4);
  28.             if (los6 > 0.5)
  29.                 zmByte = (byte) (zmByte | 1<<5);
  30.             if (los7 > 0.5)
  31.                 zmByte = (byte) (zmByte | 1<<6);
  32.             if (los8 > 0.5)
  33.                 zmByte = (byte) (zmByte | 1<<7);
  34.            
  35.             System.out.println("\n");
  36.             String stringByte = String.format("%8s", Integer.toBinaryString(zmByte & 0xFF)).replace(' ', '0');
  37.             System.out.println(stringByte);
  38.         }  
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement