Results 1 to 6 of 6
  1. #1
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default Random Number Algorithm

    Hi everyone,

    I have faced a problem with the following code which should create a random number .. could you please take a look on it??

    Java Code:
     Calendar CURRENTDATE = Calendar.getInstance ();
        int CURRENTTIME = CURRENTDATE.get (Calendar.MILLISECOND);
    
            int CURRENT9 = 50;
            int RANDNUMBER = 560;
            int NUM = CURRENTTIME + CURRENT9;
            int SIDOUT = (RANDNUMBER / CURRENT9) + NUM;
            int PIDOUT = (RANDNUMBER / NUM) + NUM;
    
                    PIDOUT = RANDNUMBER + PIDOUT;
                    SIDOUT = RANDNUMBER + SIDOUT;
                    SIDOUT = PIDOUT + SIDOUT;
                    PIDOUT = SIDOUT + PIDOUT;
                    int PID = PIDOUT / CURRENT9;
                    int PIDOUT1 = PIDOUT % CURRENT9;
                    int SID = SIDOUT / PID;
                    int SIDOUT1 = SIDOUT % PID;

    this algorithm should give me a new number in each line for example ,

    102232
    213093
    213302
    109032

    but unfortunately it gave me results same as the following which is not correct

    1233
    1233
    1233
    4323
    4323
    5334
    5334


    finally, I know I can use the random class in Java but the problem is I have to use this algorithm and I have to fix it..


    Thanks in advanced for your help,,

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Create a SSCCE (google it). If you do that you might realise what the problem is yourself. It also gives a better understanding of the purpose of the code.

    One thing I will mention is that you use Calendar class to get time and date. A common misconception is that a Calendar object is like a clock and keeps updating itself. It doesn't. It is simply a snapshot in time and will always have the same value.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Now that I think about it, you probably call a method several times. If those calls occur within a fraction of a second then they will all get the same timestamp and therefore generate the same "random" number.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep. You should do what Java does -- use the results from the most recent calculation to create a seed for the next calculation.

  5. #5
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Thanks for your help,

    But unfortunately I do not know how to do it exactly,, I know it seems strange but that what I can say,,

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    As Fubar mentioned you can use the number generated to calculate the next number. Make your RANDNUMBER variable an instance variable. At the end of the calculation set RANDNUMBER equal to the generated number. This isn't optimal but it should help to generate some different values.

Similar Threads

  1. Random number generation
    By toasty in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 11:41 AM
  2. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  3. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM
  4. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 PM
  5. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 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
  •