Is there a java equivalent of python random._urandom()? [duplicate] - java

This question already has answers here:
How to create an array of 20 random bytes?
(6 answers)
Getting random numbers in Java [duplicate]
(2 answers)
Closed 3 years ago.
If java has a function similar to the function in python called urandom from the random library how can I use it?
Python you would do:
import random
random._urandom(1)
What can I do in java for this?
Urandom doesn't generate a random numbers, it returns n random bytes suitable for cryptographic use

Use:
byte[] bytes = new byte[x];
new Random().nextBytes(bytes);
Your array will then be filled with random bytes

Related

Java: How to check if a byte[] array exists in a file? [duplicate]

This question already has answers here:
Searching for a sequence of Bytes in a Binary File with Java
(5 answers)
Closed 5 years ago.
I come up an idea to read a byte[] array with the same size of the input, and check one by one. But it seems not very efficient. Is there a way to solve it by using rolling hash?
If you are using java 8 or above please check the
java.util.Optional<T>
The documentation is here
Optional
If I got what you mean correctly

get random char in a specific range in java [duplicate]

This question already has answers here:
Is there functionality to generate a random character in Java?
(20 answers)
Closed 6 years ago.
Am designing wumpus hunting program using java. Where i can include three gold, 3 pits, one wumpus, one player. and the position of the game item should be random generator.How to generate random characters in 2d array .
How to specify the range for the characters in java.
Thanks.
You can use the method random.nextInt() to generate a random number for the position.
If you want to generate a random character you should use :
(char)(random.nextInt(max-min)+min). Where the max and min value are the ascii code of the max and the min char.
If you only want to generate a char in some char you should create an array with these character and use yourArray[random.nextInt(yourArray.length-1)+1];
Edit : You have to create the object random at the beginning of your
code. Random random = new Random();

How to convert a two bytes number in two indivudal numbers with 8 bits? [duplicate]

This question already has answers here:
Convert integer into byte array (Java)
(11 answers)
Closed 8 years ago.
Suppose i have the number 520 that is mapped in two bytes that gives me the number: 1000001000 and i want to convert this number (520) to two other numbers, these numbers should be: 2 and 8 because 00000010 will give me 2 and 00001000 will give me 8. how can i do this with java?
Like this:
int theNumber = 520;
byte oneNumber = (byte)theNumber;
byte otherNumber = (byte)(theNumber >> 8);

How to convert byte to hexadecimal string in Java [duplicate]

This question already has answers here:
Java code To convert byte to Hexadecimal
(23 answers)
Closed 9 years ago.
I have a MAC address represented as a byte[] in Java and want it as a hexadecimal string, as they are usually represented. How can I do this with as easy as possible?
The byte array has length 6.
Try this:
String hexValue = String.format("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

Generating Unique Random ArrayList of values in Java [duplicate]

This question already has answers here:
Generating Unique Random Numbers in Java
(21 answers)
Closed 9 years ago.
What would be the best way to generate 50,000 unique random values which are put into an ArrayList in Java?
Fill the ArrayList with 50,000 sequential values and call Collections.shuffle(list).

Categories

Resources