467,878 Members | 1,245 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,878 developers. It's quick & easy.

Simple question-please read!

Rob
He ,
I need to generate some random numbers, right now I am using:
Random pk = new Random();
int a = pk.nextInt(540);

and wondering if there is any other methods in java to generate random
numbers.

Thanks for any help.
Rob
Jul 17 '05 #1
  • viewed: 2744
Share:
3 Replies
Check out java.security.SecureRandom for an alternative.

It's all a matter of what you need: efficiency vs. security.

On Sat, 26 Jul 2003 14:58:00 +0000, Rob wrote:
He ,
I need to generate some random numbers, right now I am using: Random pk
= new Random();
int a = pk.nextInt(540);

and wondering if there is any other methods in java to generate random
numbers.

Thanks for any help.
Rob


--
Keep American Families united! Support H.R. 539 and H.R. 832
For more information, see http://www.kkeane.com/lobbyspousal-faq.shtml

Jul 17 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rob wrote:
He ,
I need to generate some random numbers, right now I am using:
Random pk = new Random();
int a = pk.nextInt(540);

and wondering if there is any other methods in java to generate
random numbers.

Thanks for any help.
Rob


Hi,
In addition to Ingo's suggestion that if you need secure numbers, use
SecureRandom, there is also a method java.lang.Math.random() which
returns a double from 0.0 (inclusive) to 1.0 (exclusive). It's just a
shortcut for creating a java.util.Random object and calling
nextDouble() on it. Also, if you're working with large numbers, you
could take a look at java.math.BigInteger's constructors
BigInteger(int bitLength, int certainty, Random rnd) and
BigInteger(int bitLength, Random rnd) which both generate random
prime numbers (might not be useful to you though). Of course, if you
need a securely generated BigInteger prime, just use one of those
constructors with a SecureRandom as the last parameter (since
SecureRandom extends Random).

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/I8zhwxczzJRavJYRAqHlAKDM1NWKw6St2M0DpE25cxlOyO0ZzA CgyDSQ
+oE8IrnUlNbuVZ/c02ScIa4=
=2WZy
-----END PGP SIGNATURE-----
Jul 17 '05 #3
A secure random number is one where the next random number is not
predictable from the previous one, the current system time, or some other
value.

Remember that computer-generated random numbers are never truly random
(they are technically called pseudo-random). They are always computed in
some way or another. So random numbers are always somewhat predictable
just by following the algorithm. For instance, if you use the digits of PI
as random numbers, then all you have to do to get the next one is find out
which digit of PI you used last, and then you already know what the next
one will be (e.g., if the last one was the 1765th digit, then the next one
will be the 1766th digit).

A secure random number generator uses an algorithm that makes it very
difficult to guess what the next value will be.

BTW, there also is one way of getting 100% secure random numbers: there
are hardware-based random number generators that use things like heat
oscillations, quantum effects, radioactive decay or other physical random
effects rather than pure software. Those are the only true random number
generators available for computers.

On Mon, 28 Jul 2003 20:49:57 +0000, Phil wrote:
Pls excuse ignorance on my part but what is a secure random number?

"Rob" <rs*******@hotmail.com> wrote in message
news:3f******@news.sentex.net...
Actually, my code does some simulation according the generated numbers,
I dont need them to be secured, but i need to use a different random
generation algorithm, and compare the results, right now I am using :
Random pk = new Random();
int a = pk.nextInt(540);
So I need to know if the Math.random() using a different algorithm than
above method, or if there is any other way to generate random numbers.

Thanks-Rob
"Chris" <ch*******@hotmail.com> wrote in message
news:tb********************@news1.telusplanet.net. ..
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Rob wrote:
>
> > He ,
> > I need to generate some random numbers, right now I am using:
> > Random pk = new Random();
> > int a = pk.nextInt(540);
> >
> > and wondering if there is any other methods in java to generate
> > random numbers.
> >
> > Thanks for any help.
> > Rob
>
> Hi,
> In addition to Ingo's suggestion that if you need secure numbers, use
> SecureRandom, there is also a method java.lang.Math.random() which
> returns a double from 0.0 (inclusive) to 1.0 (exclusive). It's just a
> shortcut for creating a java.util.Random object and calling
> nextDouble() on it. Also, if you're working with large numbers, you
> could take a look at java.math.BigInteger's constructors
> BigInteger(int bitLength, int certainty, Random rnd) and
> BigInteger(int bitLength, Random rnd) which both generate random
> prime numbers (might not be useful to you though). Of course, if you
> need a securely generated BigInteger prime, just use one of those
> constructors with a SecureRandom as the last parameter (since
> SecureRandom extends Random).
>
> - --
> Chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.2 (GNU/Linux)
>
> iD8DBQE/I8zhwxczzJRavJYRAqHlAKDM1NWKw6St2M0DpE25cxlOyO0ZzA CgyDSQ
> +oE8IrnUlNbuVZ/c02ScIa4=
> =2WZy
> -----END PGP SIGNATURE-----



--
Keep American Families united! Support H.R. 539 and H.R. 832
For more information, see http://www.kkeane.com/lobbyspousal-faq.shtml

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Trimbitas Sorin | last post: by
13 posts views Thread by Jason Swett | last post: by
2 posts views Thread by Anurag | last post: by
7 posts views Thread by abcd | last post: by
2 posts views Thread by Allain B√łge | last post: by
6 posts views Thread by beantaxi | last post: by
10 posts views Thread by Phillip Taylor | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.