473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
3 3035
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
13
by: Jason Swett | last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been able to tell me anything helpful. How do I do it? Any input would be greatly appreciated. Jason
2
by: Anurag | last post by:
This simple one beats me all ends up(sincerely). I have been doing DB2 UDB for some time now, reading a lot of good discussions in this forum, writing some answers, asking a lot more but this...
3
by: Brad | last post by:
I have another hopefully simple question. I am so used to writing VB .Net windows apps that there are some things in ASP .Net that just does not easily cross over. I know how to pass variables to...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
2
by: Allain Bøge | last post by:
It is really a simple question. Visual Basic .NET (2003) I create 2 forms (Form1 and Form2) I create a checkbox in Form1 (checkbox1) I create a checkbox in Form2 (checkbox1) I go to Form1...
6
by: beantaxi | last post by:
Hello all, I'm looking for a very simple code analysis tool. I have a large codebase to analyze, and all I really need to do is to find all uses of all methods in a few interfaces. Many tools...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
4
by: maheshgupta0248 | last post by:
Hi all, Im a newbie in php, started learning php on my own. I want to create small website using php, that contains links for two simple webpages > C questions > C++ questions C questions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.