如何创建一个包含20个随机字节的数组?

时间:2011-04-15 23:11:44

标签: java arrays random

如何在Java中创建一个包含20个随机字节的数组?

6 个答案:

答案 0 :(得分:242)

尝试Random.nextBytes方法:

byte[] b = new byte[20];
new Random().nextBytes(b);

答案 1 :(得分:31)

如果您想要使用加密强大的随机数生成器(也是线程安全的)而不使用第三方API,则可以使用::COMPANY NAME HERE ::MY NAME HERE - AUTOMATION INTERN :: ::An excel spreadsheet is generated containing a list of all subfiles for each project ::The spreadsheet is placed inside each project folder ::This script should run at scheduled times in order to remain updated :: @echo off ::Make working directory begin where this file is located by pushing the path onto a stack pushd %~dp0 ::Prevent echo from happening before declaration setlocal EnableDelayedExpansion set skip=0 ::Recursively access each folder inside the Projects folder for /d /r %%G in ("\BRDATA1\Projects\*") DO ( ::Change directory to current folder using short notation (to access deep folders) cd %%~sG ::Set cf to the name of the current folder set cf=%%~nG if "!cf!" == "Archive WIP" set skip=1 if "!cf!" == "Archives" set skip=1 if "!cf!" == "Help Document" set skip=1 if "!cf!" == "Recovered" set skip=1 if "!cf!" == "Templates" set skip=1 if "!cf!" == "xxxx-Customer Name" set skip=1 if "!cf!" == "xxxx-Project Name" set skip=1 if "!skip!" <> "1" ( echo Indexing Folder !cf! call :search ) set skip=0 ) ::Pop the stack echo Indexing Complete popd ::Terminate execution exit :search ::Write each filepath inside the current folder into a temporary text file ::The filepath property is used and therefore efficiency is independent from file size dir /a-d /b /s /o:gn > list.txt ::Write headers on the excel output file echo FILENAME,FILE LOCATION > index.csv ::For each filepath inside the temporary text file, Remove all text before the final \ for /f "tokens=* delims=\" %%a in (list.txt) DO ( ::Value = full path name (long) set value=%%a echo !value! >nul ::New Value = Trimmed path name and only initial project folder name uses short path name set newValue=!value:*\Projects\=! set newValue=!newValue:*\=! echo !newValue! >nul ::Write the filename including the extension, and then its trimmed path name echo %%~na%%~xa , !newValue! >> index.csv ) ::Delete the temporary text file and continue to the next project folder del "list.txt"

Java 6&amp; 7:

SecureRandom

Java 8(更安全):

SecureRandom random = new SecureRandom();
byte[] bytes = new byte[20];
random.nextBytes(bytes);

答案 2 :(得分:15)

如果您已经在使用Apache Commons Lang,那么RandomUtils会使其成为一个单行:

byte[] randomBytes = RandomUtils.nextBytes(20);

答案 3 :(得分:8)

Java 7引入了ThreadLocalRandom 与当前线程隔离

这是maerics's solution的另一个演绎。

final byte[] bytes = new byte[20];
ThreadLocalRandom.current().nextBytes(bytes);

答案 4 :(得分:3)

使用种子创建随机对象,并通过执行以下操作随机获取数组:

public static final int ARRAY_LENGTH = 20;

byte[] byteArray = new byte[ARRAY_LENGTH];
new Random(System.currentTimeMillis()).nextBytes(byteArray);
// get fisrt element
System.out.println("Random byte: " + byteArray[0]);

答案 5 :(得分:0)

对于那些想要一种更安全的方式来创建随机字节数组的人来说,最安全的方式是:

byte[] bytes = new byte[20];
SecureRandom.getInstanceStrong().nextBytes(bytes);

但如果计算机上没有足够的随机性,则线程可能会阻塞,具体取决于您的操作系统。以下解决方案将不会阻止:

SecureRandom random = new SecureRandom();
byte[] bytes = new byte[20];
random.nextBytes(bytes);

这是因为第一个示例使用/dev/random,并且在等待更多随机性时会阻塞(由鼠标/键盘和其他来源生成)。第二个示例使用/dev/urandom,它不会阻塞。