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

    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
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default

    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,

    Jos
    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
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    11

    Default

    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, 05:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02: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
  •