array to generate words

I

IanH

I need to generate a message box with each of these random words but
cant figure out what im doing wrong, can anyone help?


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);

}

}
 
O

Oliver Wong

IanH said:
I need to generate a message box with each of these random words but
cant figure out what im doing wrong, can anyone help?


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
 
R

Rhino

IanH said:
I need to generate a message box with each of these random words but
cant figure out what im doing wrong, can anyone help?


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);

}

}
If I understand you correctly, you are populating the 'text' array with five
Strings and want to choose one of them at random and display that String. If
that is right, the following untested code should do the job:

/* 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]);
 
I

IanH

Hi 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.
 
O

Oliver Wong

IanH said:
Hi 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
 
J

James Westby

IanH said:
Hi 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.
try

JOptionPane.showMessageDialog(null, text[increment]);


James
 
R

Rhino

Rhino said:
IanH said:
I need to generate a message box with each of these random words but
cant figure out what im doing wrong, can anyone help?


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);

}

}
If I understand you correctly, you are populating the 'text' array with
five Strings and want to choose one of them at random and display that
String. If that is right, the following untested code should do the job:

/* 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]);
Oops, that last line should be:

JOptionPane.showMessageDialog(null, "The random day is " +
text[randomIndex]);
 
I

IanH

James said:
IanH said:
Hi 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.
try

JOptionPane.showMessageDialog(null, text[increment]);


James
 
I

IanH

Thanks Rhino

I have a problem with the Random random = new random, am i supposed to
have a class created for this?
 
D

Daniel Dyer

If I understand you correctly, you are populating the 'text' array with
five
Strings and want to choose one of them at random and display that
String. If
that is right, the following untested code should do the job:

/* 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]);

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.
 
R

Rhino

IanH said:
Thanks Rhino

I have a problem with the Random random = new random, am i supposed to
have a class created for this?
No, that class already exists. You'll need an import for it at the start of
your class. The full name of the class is java.util.Random.
 
R

Rhino

Daniel Dyer said:
If I understand you correctly, you are populating the 'text' array with
five
Strings and want to choose one of them at random and display that
String. If
that is right, the following untested code should do the job:

/* 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]);

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.
I stand corrected.
 
D

Daniel Dyer

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.

Dan.
 
F

Filip Larsen

Daniel Dyer wrote
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?
 
C

Chris Uppal

Daniel said:
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
 
D

Daniel Dyer

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 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.

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top