Isizukulwane seJava esingaQhelekanga

Ukuvelisa iinombolo ezingahleliwe kwiJava ngumsebenzi oqhelekileyo. Umzekelo, unokufuna ukwenza uvavanyo olunamaxabiso angahleliwe ngalo lonke ixesha. Kule posi, sijonga iindlela ezahlukeneyo esinokuvelisa ngazo iinombolo ezingahleliwe kwiJava.



Ungawenza njani amaNani amaRandi kwiJava

KwiJava, sinokuvelisa amanani ngokungahleliwe ngokusebenzisa java.util.Random iklasi.

Nje ukuba singenise iklasi engaQhelekanga, sinokwenza into kuyo enika amandla okusebenzisa amanani angaqhelekanga.


Umzekelo, iindlela nextInt() kunye nextLong() izakubuyisa inani eliphakathi kwamaxabiso (angalunganga kunye noqinisekileyo) weent kunye nedatha ende ngokulandelelana.



Ukwenza i-Int engahleliwe, ixesha elide, kunye neBoolean

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());
} }


Ukudala iinombolo ezingaQhelekanga kuLuhlu

Ngamanye amaxesha, sifuna iinombolo ezingahleliwe ziveliswe kuluhlu oluthile, o.k.t. phakathi ko-1 no-50.


Ukwenza oku, sinokubonelela ngeparameter epheleleyo kwi nextInt() indlela. Le parameter ichaza umda ophezulu kuluhlu.

Inye kuphela into ekufuneka uyiqaphele apha kukuba inani eliqingqiweyo elingaphezulu alifakwanga njengenye yamanani avelisiweyo. Umzekelo, nextInt(5) ivelisa amanani ukusuka 0 ukuya 4 kubandakanya.

Ukuba nathi sinqwenela 5 ukuba kuluhlu lweenombolo ezingahleliwe, kufuneka sisebenzise 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);
} }


Ukwenza iiNombolo eziQhelekileyo eziQhelekileyo kwiJava

Udidi oluhleliweyo luvelisa amanani akhethiweyo ngendlela yokuchonga. Ialgorithm evelisa into engahleliwe isekwe kwinani elibizwa ngokuba yimbewu. Ukuba inani lembewu liyaziwa emva koko kunokwenzeka ukuba ufumane amanani azakuveliswa kwialgorithm.


Injongo ye SecureRandom Iklasi kukuvelisa amanani akhethiweyo ngokungaxutywanga.

I SecureRandom kufuneka ivelise imveliso engamiselwanga. Ke ngoko nasiphi na isixhobo sembewu esidluliselwe kwi- SecureRandom into ayinakulindeleka.

Apha ngezantsi kukho umzekelo wokusetyenziswa kwe SecureRandom iklasi ukuvelisa iinombolo ezingahleliwe kwiJava

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);
} }

Le mizekelo ingentla ibonisa indlela yokuvelisa iinombolo ezingahleliwe kwiJava.


Isalathiso: Ukukhuselwa kwamanani aQhelekileyo