Results 1 to 12 of 12

Thread: Random

  1. #1
    koolhoney is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default Random

    hi

    please help me in generating the random numbers.

    I have five methods in different classes, i want to get the same random number in all methods. can anybody help me.

    Is Inheritance requred amoung the class?
    please give some idea.

    Thanks in adv

  2. #2
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    Hey, to generate random numbers you can declare a variable,


    for instance

    public class Random
    {
    private int Number1;

    public Random()
    {
    Number1 = (int)(Math.random() * 10);
    //Generates a value from (0.1 to 1.0) * 10 thus numbers 1-10
    }
    public int getNumber1()
    {
    return Number1;
    }

    But that's assuming it's not the main if it's the main then:

    public class Main
    {
    public static void main(String[]args)
    {

    //Now make objects of your classes
    //For instanse

    Class1 cs = new Class1();

    //Generate a number

    int x = (int)(Math.random() * 10);

    //Now put it in the method you want in the class by

    cs.setmethodName(x);

    System.out.println("The number is "+ cs.getmethodName());
    }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default Really?

    Wow, thanks for the info Eranga!;)
    So, what's the maximum 0.9?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    koolhoney is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    Hi
    Thanks for your reply.
    shall i use the setter and getter methods to store that number andshall i get the same random number in 5 different methods in didfferent classes by calling tha getmethod. Is it posiible to any on-line sites(like any registreation or booking sites)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    koolhoney is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    please give proper way to do that.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    koolhoney is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    I have to generate the number randomly in one method and that value should reflect in 5 more methods, in same or another class.
    For that i have to store in one setmethod and get method.

    Now should i call this getmethod in another method to maintain the same random value in all 5 methods.

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Smile Put randomizer in one class

    Start by writing one class that does one thing, generate random numbers. Then have the other class constructors take a variable of type class you wrote - just make sure all classes get the instance from the same call to new if all classes need the random numbers to match up, get and set may need to be called generate / get as one would not likely set the random number from other classes. Sometimes this may be called producer-consumer pattern. How many random numbers are needed and to they need to match up in all places?

    Code from Loop should get you going, with note from eranga that random only generates values < 1.0000000 eg 0.999897758 or something. Simple but takes a moment to grasp and get it right.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by koolhoney View Post
    I have to generate the number randomly in one method and that value should reflect in 5 more methods, in same or another class.
    For that i have to store in one setmethod and get method.

    Now should i call this getmethod in another method to maintain the same random value in all 5 methods.
    If you want to use the same randomly generated number in five different locations, you have to use get and set method. It's nothing to say that if you call the random number generating method five times, you get five different values.

    But first of all, as Nichol says in his last post, deal with a one class first and see how it's easy/difficult to implement in your application.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 10:47 PM
  3. Random problem
    By mcal in forum New To Java
    Replies: 18
    Last Post: 01-24-2008, 04:00 AM
  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 generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 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
  •