Package com company; import java nio charset. StandardCharsets; import


Download 14.13 Kb.
Sana26.11.2020
Hajmi14.13 Kb.
#152613
Bog'liq
lab 6

    Bu sahifa navigatsiya:
  • UTF_8

package com.company;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

class GFG{
// Function to generate random numbers
static void linearCongruentialMethod(int Xo, int m,
int a, int c,
int[] randomNums,
int noOfRandomNums)
{// Initialize the seed state
randomNums[0] = Xo;
// Traverse to generate required
// numbers of random numbers
for(int i = 1; i < noOfRandomNums; i++)
{// Follow the linear congruential method
randomNums[i] = ((randomNums[i-1] * a) + c) % m;
} }
// Driver code
public static void main(String[] args)
{// Seed value
Scanner km=new Scanner(System.in);
System.out.print("Dastlabki kalitni kiriting ");
int Xo = km.nextInt();
// Modulus parameter
System.out.print(" baza chegarasi yozing ");
int m = km.nextInt();
// Multiplier term
int a = 6;
// Increment term
int c = 1;
// Number of Random numbers
// to be generated
System.out.print("Eslatma! kalit uzunligi 8ga karrali bo'lsin\nkalit uzunligi(bitlarda)");
int kk=km.nextInt();
int noOfRandomNums = kk/8;
// To store random numbers
int[] randomNums = new int[noOfRandomNums];
// Function Call
linearCongruentialMethod(Xo, m, a, c,
randomNums,
noOfRandomNums);
// Print the generated random numbers
for(int i = 0; i < noOfRandomNums; i++)
{
System.out.print(randomNums[i] + " ");
String input=String.valueOf(randomNums[i]);
String result=convertByteArraysToBinary(input.getBytes(StandardCharsets.UTF_8));
System.out.print(result+" ");
}
}

//====================================================

public static String convertByteArraysToBinary(byte[] input) {

StringBuilder result = new StringBuilder();


for (byte b : input) {
int val = b;
for (int i = 0; i < 8; i++) {
result.append((val & 128) == 0 ? 0 : 1); // 128 = 1000 0000
val <<= 1;
}
}
return result.toString();

}}

// This code is contributed by offbeat
Download 14.13 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling