Results 1 to 5 of 5

Thread: Random

  1. #1
    Feilin is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Random

    How do I make a (I don't know the term for it) function (?) that gives a random number and that I can use unlimitedly in a class (without writing the whole code for the randomising sequence)?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Feilin View Post
    How do I make a (I don't know the term for it) function (?) that gives a random number and that I can use unlimitedly in a class (without writing the whole code for the randomising sequence)?
    There is the 'Random' class (read its API documentation) and there's the Math.random() method that can give you a pseudo random number.

    Finally there's this site: http://www.fourmilab.ch/hotbits that can give you true random numbers.

    kind regards,

    Jos
    Last edited by JosAH; 08-15-2010 at 03:26 PM.

  3. #3
    Feilin is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    If I wanted to make my own randomiser, say because I wanted my own seed, how can I make that into a class and where should I put it? (I'm using Eclipse btw, if that matters)

    Also, I'd like to save the result each time I use it to a specific number, which I can access by something like result(i), where "i" is the number in order for the given instance, how do I do that?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I'd like to save the result
    Save it in some kind of collection object like an ArrayList. It has methods to return the items it contains based on their order.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Feilin View Post
    If I wanted to make my own randomiser, say because I wanted my own seed, how can I make that into a class and where should I put it? (I'm using Eclipse btw, if that matters)

    Also, I'd like to save the result each time I use it to a specific number, which I can access by something like result(i), where "i" is the number in order for the given instance, how do I do that?
    I'd use one of the available pseudo random number generators and a List<Double> for the already generated numbers. A few additional methods can handle the rest of it. I'd use a separate object for a given seed. The skeleton of the class would look like this:

    Java Code:
    public class Rand {
       private List<Double> memory= new ArrayList<Double>();
       private Random r;
       public Rand(long seed) { r= new Random(seed); }
       ...
    }
    kind regards,

    Jos

    kind regards,

    Jos

Similar Threads

  1. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Random
    By koolhoney in forum New To Java
    Replies: 11
    Last Post: 09-30-2008, 03:39 AM
  4. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 10:47 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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