import javax.swing.*;
public class random
{
public static void main (String args[])
{
String text[];
text = new String[5];
string increment;
int number = 0;
text[number++] = "Monday";
text[number++] = "Tuesday";
text[number++] = "Wednesday";
text[number++] = "Thursday";
text[number++] = "Friday";
increment = Math.floor(Math.random() * number);
JOptionPane.showMessageDialog(null, increment);
}
}
Did you try actually running your program? What output does it produce?
Is this the desired output? If not, where would the source of the erroneous
output be coming from? For example, fi the program is printing out "Canada",
"America", "Germany", etc. instead of days of the week, I'd be looking at
the parts of my code which contain names of countries, and ask myself how
did that get into the data flow leading to the message dialog.
What is it outputting, and where might that data becoming from?
- Oliver
I know its not outputting correctly, the output is numbers instead of
the days of the week. Is this to do with number++, not sure new to java
so appreciate suggestions.
Well, you can try an experiment. Remove all the number++, replacing them
with something else (perhaps 0 or something), and see if that affects the
output.
Another technique would be to pretend to be the computer, and read the
source code and try to decide what you would do at each step if you were the
computer, to try to spot the problem.
- Oliver
JOptionPane.showMessageDialog(null, text[increment]);
James
/* Create random number generator. */
Random random = new Random();
/* Choose an integer between 0 and the size of the array; effectively
chooses the index of one element of the array. */
int randomIndex = Math.abs(random.nextInt()%text.length);
/* Display the randomly-chosen day. */
JOptionPane.showMessageDialog(null, "The random day is " + text[randomInt]);
--
Rhino
JOptionPane.showMessageDialog(null, "The random day is " +
text[randomIndex]);
--
Rhino
I have a problem with the Random random = new random, am i supposed to
have a class created for this?
The randomIndex would be better to use the nextInt method that takes a
parameter:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html#nextInt(int)
The main reason in this case is because it's simpler, but you should also
be aware that performing modulo arithmetic on random numbers introduces
bias.
Dan.
--
Daniel Dyer
http://www.dandyer.co.uk
--
Rhino
--
Rhino
> Math.abs(random.nextInt()%text.length);
Don't do that. There are much better ways. See
http://mindprod.com/jgloss/pseudorandom.html#STRINGS
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
>I have a problem with the Random random = new random, am i supposed to
>have a class created for this?
see sample code at
http://mindprod.com/jgloss/pseudorandom.html#STRINGS
The example that generates a random fruit could be modified trivially
to create a day of the week just by substituting the fruit names with
names of days of the week.
> On Thu, 2 Mar 2006 13:43:16 -0500, "Rhino"
> <no.offline.c...@nospam.com> wrote, quoted or indirectly
> quoted someone who said :
>
>> Math.abs(random.nextInt()%text.length);
>
> Don't do that. There are much better ways. See
> http://mindprod.com/jgloss/pseudorandom.html#STRINGS
Roedy, that's a very thorough page. There's one piece of information that
might be useful to add, though not at all relevant to the original
discussion in this thread is a note about performance.
I have found that the SecureRandom class is considerably slower under
Linux than it is on Windows (my benchmarks take about 8 times as long to
complete on the same hardware), and even on Windows it's significantly
slower than any other Java RNG implementation. This makes it unusable for
any simulations that require a large quantity of statistically sound
random numbers. With java.util.Random failing to produce statistically
random output, any non-trivial use of random numbers in Java needs to use
a non-core RNG implementation.
>I have found that the SecureRandom class is considerably slower under
>Linux than it is on Windows
noted.
> I have found that the SecureRandom class is considerably slower under
> Linux than it is on Windows (my benchmarks take about 8 times as long
to
> complete on the same hardware), and even on Windows it's significantly
> slower than any other Java RNG implementation.
If I remember correctly, the SecureRandom potentially uses a lot of time
during initialization to "collect entropi". Can you possibly state
whether or not generator initialization is included in the factor 8 you
mention above?
> With java.util.Random failing to produce statistically
> random output, any non-trivial use of random numbers in Java needs to use
> a non-core RNG implementation.
Searching for:
"mersenne twister" java
produces lots of promising-looking hits. (Not limited to implementations of
that particular PRNG)
-- chris
I have produced my own direct Java port of the original C and the
peformance is quite impressive even though I have made no attempt to
optimise it. The throughput is better than java.util.Random and the
output is much more random according to the Diehard tests.
That's a good point and something I should have considered. My benchmark
does not include the time taken to instantiate the SecureRandom object,
but I guess it's possible that the entropy gathering is deferred until the
first request is made (I haven't looked at the SecureRandom source). I'll
try it again later but make a few requests before I start timing and see
what the difference is. The benchmark generates 1 million random ints, 1
million random doubles and 1 million Gaussians. For SecureRandom it takes
less than 4 seconds under Windows and about 30 seconds under Linux on my
laptop. If the SecureRandom is getting its entropy from /dev/random it
shouldn't take 26 seconds. My guess, and that's all it is, is that it
something to do with the SHA-1 implementation in the Linux version of
JCE. I have written an RNG that uses the AES block cipher and seeds
itself from /dev/random and it completes the benchmark in under 2 seconds
under Linux.
[me:]
> > Searching for:
> > "mersenne twister" java
> > produces lots of promising-looking hits. (Not limited to
> > implementations of that particular PRNG)
> I have produced my own direct Java port of the original C
Blimey, that was quick ;-)
> and the
> peformance is quite impressive even though I have made no attempt to
> optimise it. The throughput is better than java.util.Random and the
> output is much more random according to the Diehard tests.
How does using something like RC4 compare for speed/quality ?
-- chris
> Daniel Dyer wrote:
>
> [me:]
>> > Searching for:
>> > "mersenne twister" java
>> > produces lots of promising-looking hits. (Not limited to
>> > implementations of that particular PRNG)
>
>> I have produced my own direct Java port of the original C
>
> Blimey, that was quick ;-)
I meant previously :)
The C code, and therefore my Java port also, is a whole bunch of magic
numbers and bit manipulations. I've no idea how it actually works.
>
>> and the
>> peformance is quite impressive even though I have made no attempt to
>> optimise it. The throughput is better than java.util.Random and the
>> output is much more random according to the Diehard tests.
>
> How does using something like RC4 compare for speed/quality ?
I've used AES. The quality is the same according to Diehard, but the
Mersenne Twister is about 3 times faster. The AES implementation is still
twice as fast as SecureRandom though. I read somewhere that a non-linear
generator like AES is preferable to a linear generator like the Mersenne
Twister for certain types of simulation, though I don't know the details.
> My benchmark
> does not include the time taken to instantiate the SecureRandom
object,
> but I guess it's possible that the entropy gathering is deferred until
the
> first request is made (I haven't looked at the SecureRandom source).
I'll
> try it again later but make a few requests before I start timing and
see
> what the difference is. The benchmark generates 1 million random
ints, 1
> million random doubles and 1 million Gaussians. For SecureRandom it
takes
> less than 4 seconds under Windows and about 30 seconds under Linux on
my
> laptop.
Doing that test with Java 1.4.2, I get the relative speed of Random to
the SecureRandom (Sun provider) at around 3 to 8 depending on OS and
whether -server or -client is used. Best was server on Windows where
SecureRandom ran the test only 3 times as slow as Random (3062 ms vs.
1047 ms). Worst was client on Windows with a factor 8 (5078 ms vs. 625
ms). In the Linux test the ratios were between 6.5 (server) to 8
(client). The Linux hardware was different but the test ran in times
comparable to the Windows test. Instantiation and warm-up of Sun's
SecureRandom took around 200-800 ms in all cases.
So my guess would be that the 30 seconds you see on Linux must be an
issue not related to the actual generation of numbers. Perhaps the
generator is not seeding from /dev/random or perhaps its a different
provider with a non-performing implementation?
Regards,
--
Filip Larsen
I checked what implementation was being used on Linux by default and it
is still the Sun provider but it uses an algorithm called "NativePRNG",
which appears to be much slower than the non-native default. So my
previous advice about performance on Linux should be qualified with
this information.
SecureRandom is still slow, even on Windows, it's just not necessarily
as slow as I stated.
Just to avoid any confusion, this post is from me. I went looking for the
thread on Google Groups and posted the reply on there. I thought it would
be clever enough to use the name I gave it in the "From" field.
Note thought that while Mersenne Twister produces a nice sequence of
random numbers for simulations, it is not cryptographically secure; that is,
an attacker can easily[*] recognize an MT-generated sequence, and predict
the next elements.
Use MT for games and simulations, but not for encryption, passwords,
etc.
- Oliver
[*] Where "easily" means "relative to random number generators which ARE
considered cryptographically secure".
> Note thought that while Mersenne Twister produces a nice sequence of
> random numbers for simulations, it is not cryptographically secure; that
> is, an attacker can easily[*] recognize an MT-generated sequence, and
> predict the next elements.
That why I (idly) asked Daniel if he'd tested the performance of RC4 used as a
PRNG. It should be pretty fast, but I haven't got around to measuring it
myself. OTOH, I don't know how well RC4 approximates to "random" -- it could
be very non-random but still secure (e.g. if it doubled-up every output byte
;-)
-- chris
>
> "Chris Uppal" <chris...@metagnostic.REMOVE-THIS.org> wrote in message
> news:44097849$1$1176$bed6...@news.gradwell.net...
>> Daniel Dyer wrote:
>>
>>> With java.util.Random failing to produce statistically
>>> random output, any non-trivial use of random numbers in Java needs to
>>> use
>>> a non-core RNG implementation.
>>
>> Searching for:
>> "mersenne twister" java
>> produces lots of promising-looking hits. (Not limited to
>> implementations of
>> that particular PRNG)
>
> Note thought that while Mersenne Twister produces a nice sequence of
> random numbers for simulations, it is not cryptographically secure; that
> is, an attacker can easily[*] recognize an MT-generated sequence, and
> predict the next elements.
>
> Use MT for games and simulations, but not for encryption, passwords,
> etc.
>
Indeed. Away from work one of my interests is evolutionary algorithms.
The Mersenne Twister is excellent for these because of its statistical
qualities and blinding speed. At work I'm mostly involved in the online
gambling industry. We don't use algorithms like the Mersenne Twister for
these for precisely the reason you mention. Although it's very unlikely,
if an attacker could observe all of the random output of the RNG for a
long enough sequence, they could relatively easily reverse engineer the
seed and predict all future output of the RNG, thus losing our clients
lots of money.