array to generate words

Discussion in 'Java' started by IanH, Mar 2, 2006.

  1. IanH

    IanH Guest

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

    }

    }
    IanH, Mar 2, 2006
    #1
    1. Advertising

  2. IanH

    Oliver Wong Guest

    "IanH" <> wrote in message
    news:...
    >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
    Oliver Wong, Mar 2, 2006
    #2
    1. Advertising

  3. IanH

    Rhino Guest

    "IanH" <> wrote in message
    news:...
    >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]);

    --
    Rhino
    Rhino, Mar 2, 2006
    #3
  4. IanH

    IanH Guest

    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.
    IanH, Mar 2, 2006
    #4
  5. IanH

    Oliver Wong Guest

    "IanH" <> wrote in message
    news:...
    > 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
    Oliver Wong, Mar 2, 2006
    #5
  6. IanH

    James Westby Guest

    IanH wrote:
    > 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
    James Westby, Mar 2, 2006
    #6
  7. IanH

    Rhino Guest

    "Rhino" <> wrote in message
    news:71HNf.4706$...
    >
    > "IanH" <> wrote in message
    > news:...
    >>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]);

    --
    Rhino
    Rhino, Mar 2, 2006
    #7
  8. IanH

    IanH Guest

    James Westby wrote:

    > IanH wrote:
    > > 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
    IanH, Mar 2, 2006
    #8
  9. IanH

    IanH Guest

    Thanks Rhino

    I have a problem with the Random random = new random, am i supposed to
    have a class created for this?
    IanH, Mar 2, 2006
    #9
  10. IanH

    Daniel Dyer Guest

    On Thu, 02 Mar 2006 18:43:16 -0000, Rhino
    <> wrote:
    > 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.


    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Mar 2, 2006
    #10
  11. IanH

    Rhino Guest

    "IanH" <> wrote in message
    news:...
    > 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.

    --
    Rhino
    Rhino, Mar 2, 2006
    #11
  12. IanH

    Rhino Guest

    "Daniel Dyer" <> wrote in message
    news:eek:...
    > On Thu, 02 Mar 2006 18:43:16 -0000, Rhino
    > <> wrote:
    >> 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.

    --
    Rhino
    Rhino, Mar 2, 2006
    #12
  13. IanH

    IanH Guest

    Cheers for the help, running perfect now.
    IanH, Mar 2, 2006
    #13
  14. IanH

    Roedy Green Guest

    Roedy Green, Mar 3, 2006
    #14
  15. IanH

    Roedy Green Guest

    On 2 Mar 2006 11:23:24 -0800, "IanH" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 3, 2006
    #15
  16. IanH

    Daniel Dyer Guest

    Random numbers (was Re: array to generate words)

    On Fri, 03 Mar 2006 00:56:31 -0000, Roedy Green
    <> wrote:

    > On Thu, 2 Mar 2006 13:43:16 -0500, "Rhino"
    > <> 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.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Mar 3, 2006
    #16
  17. IanH

    Roedy Green Guest

    Re: Random numbers (was Re: array to generate words)

    On Fri, 03 Mar 2006 21:33:24 -0000, "Daniel Dyer"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >I have found that the SecureRandom class is considerably slower under
    >Linux than it is on Windows

    noted.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 3, 2006
    #17
  18. IanH

    Filip Larsen Guest

    Re: Random numbers (was Re: array to generate words)

    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?
    Filip Larsen, Mar 4, 2006
    #18
  19. IanH

    Chris Uppal Guest

    Re: Random numbers (was Re: array to generate words)

    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)

    -- chris
    Chris Uppal, Mar 4, 2006
    #19
  20. IanH

    Daniel Dyer Guest

    Re: Random numbers (was Re: array to generate words)

    On Sat, 04 Mar 2006 11:14:23 -0000, Chris Uppal
    <-THIS.org> wrote:

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


    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.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Mar 4, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Strøiman
    Replies:
    1
    Views:
    2,064
    Peter Strøiman
    Aug 23, 2005
  2. Richard Heathfield
    Replies:
    7
    Views:
    346
    Barry Schwarz
    Oct 5, 2003
  3. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    412
    Daniel T.
    Feb 16, 2006
  4. BerlinBrown
    Replies:
    6
    Views:
    4,416
  5. pantagruel
    Replies:
    8
    Views:
    420
    Dr John Stockton
    Jul 22, 2006
Loading...

Share This Page