Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Random Number Arrays

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Number Arrays

    Ok so im trying to create two arrays of different random numbers, the problem is both arrays have exaclty the same numbers, now i have tried using one Random and the setting different seeds before generating the numbers but this does not help.

    Heres the code

    Random rnd = new Random();
     
        //Create tempchannels for scene1
        for(int i=0;i<=511;i++){
          tempChannels[i] = rnd.nextInt(255);
        }
        scene1 = new Scene(tempChannels);
     
        //Create tempchannels for scene1
        for(int i=0;i<=511;i++){
          tempChannels[i] = rnd.nextInt(255);
        }   
        scene2 = new Scene(tempChannels);
     
        compScene1 = scene1.getChannels();
        compScene2 = scene2.getChannels();


  2. #2
    Member jbarke12's Avatar
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    31
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Default Re: Random Number Arrays

    I bet if you defined two different arrays to pass into your scenes you wouldn't have this problem

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Number Arrays

    Thats great it worked, but now to be awkward i want to understand why my original logic didnt work. If im setting all 512 parts of the array to a random number then assigning that to scene1, then i overwriting the array and assigning that to scene2 why did the numbers come out the same? The random number seed is set at the start so once it has generate the first 512 numbers the second 512 wuld essentially be continuing from the pointnit left off. But it seems to be restarting from the same seed point, if that makes sense.

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Random Number Arrays

    im setting all 512 parts of the array to a random number then assigning that to scene1, then i overwriting the array and assigning that to scene2
    How much arrays do you manipulate during this operation?

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Number Arrays

    Three arrays mainly. One to hold the random numbers generated that is then passed into a new object of scene, ie scene1. This same array is then has its values overwritten and passed to a new object of scene ie scene2 I then use two more arrays to read the arrays from the each object ie scene1 and scene2

  6. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Random Number Arrays

    Do you remember, that you pass to the methods not the arrays themselves, but merely references to the array location in JVM heap?

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Random Number Arrays

    Of course , how silly of me to make such a mistake, cheers for the help

Similar Threads

  1. Random number
    By Imran Ahmad in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2012, 12:53 PM
  2. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 05:35 AM
  4. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 09:50 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 07:10 AM