Results 1 to 3 of 3
  1. #1
    Salamander's Avatar
    Salamander is offline Member
    Join Date
    Aug 2010
    Waxahachie TX USA
    Rep Power

    Default Is Random() Only For Numbers?

    I have just recently begun to play around with the "Random()" utility in Java, though I have known about it for awhile now.

    All to tutorials I have followed the taught about random() dealt with numbers.

    What I am trying to build is a random quote generator which involves arrays and strings.

    Basically what I'm asking is: is there a way this can work? or is Random() only for numbers?

    (I did something similar in JavaScript a few years ago)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Yes, a Random object can only generate pseudo random numbers; but your problem doesn't really exist though: if you have n quotes simply generate a pseudo random number in the range [0, n) (see the nextInt(...) method) and select that particular quote.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Zurich, Switzerland
    Rep Power


    Quote Originally Posted by Salamander View Post
    Basically what I'm asking is: is there a way this can work?

    you can store your strings in collection that implements a list or an array and then generate a random number and use this number to get a random string from your collection with get(int index) or array[index]. so there is no need that Random() generate something else than numbers.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 06:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 03:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts