English 中文(简体)
独一随机数字生成
原标题:unique random number generation
  • 时间:2012-05-26 13:48:32
  •  标签:
  • java

I m looking for way to generate unique random numbers based on current time using java. I m a c++ programmer and in c++ I usually seed the Random with time so at each specific moment I can get a unique random number and it works like this :

sRand((time)Null);
x=Rand();

在Java,我发现我可以使用同样的方法, 播种随机数 和这样的时间:

Random rand = new Random(System.currentTimeMillis());

Here is the problem I have used all the methods I found over the internet for generating a random number in java but none of them were truly random and they ranged from negative to positive numbers. For example:

Random rand = new Random(System.currentTimeMillis());
int x=rand.nextInt(); // or long or float ...

我得到的是一系列并非真正随机的数字,结果与C++中的结果完全不同。

我只是想知道 在Java里做这件事的最好方法是什么, 一些类似或非常接近TAC数字的一代。

问题回答

Java s 和 C+++ 随机数字都 < a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator" @em>psedorandom 。当然,算法是不同的,结果也不同。

如果您想要一个能够用于加密的随机数字生成器, 您可以使用 < a href=" http://docs. oracle.com/javase/6/docs/api/java/securureRandom.html"\ code > SecureRandom : 其界面不那么直观,它消耗的CPU更多,但其输出质量远高于爪哇州普通PRNG,

如果您想要生成一个 irandom 编号 , 最简单的方式就是使用新的 Random (. nextInt ()), 也许用 Math.abs () 包起来以避免负数。 如果您需要的是 unique number , 您可以使用 UUUID. randomUID ()。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...