Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //cw06z03
- //napisz program ktory utworzy zmienna typu byte gdzie kazdy bit bedzie losowany
- //jezeli wynikiem losowania bedzie 0,5 i wiecej wstaw 1, jezeli mniej to 0
- public class cw06z03
- {
- public static void main ( String[] args)
- {
- byte zmByte = 0;
- double los1 = Math.random();
- double los2 = Math.random();
- double los3 = Math.random();
- double los4 = Math.random();
- double los5 = Math.random();
- double los6 = Math.random();
- double los7 = Math.random();
- double los8 = Math.random();
- if (los1 > 0.5)
- zmByte = (byte) (zmByte | 1<<0);
- if (los2 > 0.5)
- zmByte = (byte) (zmByte | 1<<1);
- if (los3 > 0.5)
- zmByte = (byte) (zmByte | 1<<2);
- if (los4 > 0.5)
- zmByte = (byte) (zmByte | 1<<3);
- if (los5 > 0.5)
- zmByte = (byte) (zmByte | 1<<4);
- if (los6 > 0.5)
- zmByte = (byte) (zmByte | 1<<5);
- if (los7 > 0.5)
- zmByte = (byte) (zmByte | 1<<6);
- if (los8 > 0.5)
- zmByte = (byte) (zmByte | 1<<7);
- System.out.println("\n");
- String stringByte = String.format("%8s", Integer.toBinaryString(zmByte & 0xFF)).replace(' ', '0');
- System.out.println(stringByte);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement