Java Random Nhamba Yezvizvarwa

Kugadzira manhamba asina kujairika muJava ibasa rakajairika. Semuenzaniso, iwe ungangoda kuita bvunzo nemaitiro akasarudzika nguva imwe neimwe. Mune ino posvo, isu tinotarisa nzira dzakasiyana dzatinogona kuburitsa nhamba dzakasarudzika muJava.

Maitiro ekugadzira Random manhamba muJava

MuJava, tinogona kugadzira nhamba dzisingaenzaniswi tichishandisa java.util.Random kirasi.

Kana tangopinza iyo Random kirasi, tinogona kugadzira chinhu kubva pairi chinotipa kugona kushandisa zvisina kujairika manhamba.

Semuenzaniso, nzira nextInt() uye nextLong() inodzosera iyo nhamba iri mukati meyakaenzana yemitengo (isina kunaka uye yakanaka) yeiyo int uye marefu data mhando zvakateedzana.

Kugadzira zvisina kujairika Int, Yakareba, uye Boolean

import java.util.Random; public class GenerateRandomNumbers {
static Random rand;
public static void main(String[] args) {
rand = new Random();
System.out.println('Random integer: ' + rand.nextInt());
System.out.println('Random long: ' + rand.nextLong());
System.out.println('Random boolean: ' + rand.nextBoolean());
} }

Kugadzira Random manhamba muRange

Dzimwe nguva, isu tinoda kuti nhamba dzakasarudzika dzibudiswe kubva kune imwe nhanho, i.e. pakati pe1 ne50.

Kuti tiite izvi, tinogona kupa iyo yakazara paramende kune nextInt() nzira. Iyi paramende inotsanangura muganho wepamusoro weiyo renji.



Chinhu chimwe chete chekucherechedza apa ndechekuti iyo yepamusoro muganho nhamba haina kusanganiswa seimwe yenhamba dzinogadzirwa. Semuenzaniso, nextInt(5) inogadzira manhamba kubva 0 ku 4 inosanganisira.

Kana isu tichidawo 5 kuti uve mune runyorwa rwehuwandu hwehuwandu, tinoda kushandisa nextInt(5)+1

import java.util.Random; public class GenerateRandomNumbers {
static Random rand;
public static void main(String[] args) {
rand = new Random();
int randInt = rand.nextInt(5) + 1;
System.out.println('Random integer: ' + randInt);
} }

Kugadzira Yakachengeteka Random Manhamba muJava

Iyo Random kirasi inogadzira yakasarudzika manhamba nenzira yekusarudza. Iyo algorithm inoburitsa yakasarudzika inoenderana nenhamba inonzi mhodzi. Kana iyo nhamba yembeu ichizivikanwa saka zvinokwanisika kuyera nhamba dzichazogadzirwa kubva kualgorithm.

Chinangwa che SecureRandom Kirasi ndeyokuburitsa yakajeka yakajeka manhamba.

I SecureRandom inofanirwa kuburitsa isiri-yekufungidzira kuburitsa. Naizvozvo chero mbeu yemidziyo yakapfuudzwa kune SecureRandom chinhu chinofanira kunge chisingatarisirwe.

Pazasi pane muenzaniso mashandisiro e SecureRandom kirasi kuburitsa zvisina kujairika manhamba muJava

import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; public class GenerateRandomNumbers {
static SecureRandom secureRandomGenerator;
public static void main(String[] args) {


try {

secureRandomGenerator = SecureRandom.getInstance('SHA1PRNG', 'SUN');
}

catch (NoSuchAlgorithmException | NoSuchProviderException e) {
}

//Get random integer in range
int randInRange = secureRandomGenerator.nextInt(499);
System.out.println(randInRange);
} }

Iyi mienzaniso iripamusoro inoratidza maitiro ekugadzira nhamba dzisingaite muJava.

Referensi: Yakachengeteka Random Nhamba Chizvarwa