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 4 of 4

Thread: I dont know what to call this tittle.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I dont know what to call this tittle.

    Hi i need some help. I have been using ++ or -- for example i++. The problem is i would like my values to get picked at random thru my array. using ++ or -- is to predictable with what im doing. What formula can i use to pick random arrays instead of ++ or -- that go up one or down one?


  2. #2
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: I dont know what to call this tittle.

    Well... I don't know if this would be of much use to you, but maybe you can change it to what you need??

    (your array) = (int) (Math.random() * 10)

    I'm very new so i'm not sure if you can change that to select a random number in your array if your array was 10 numbers long.

    Hope this helps in some way, shape, or form!

    Good luck
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: I dont know what to call this tittle.

    Quote Originally Posted by miller4103 View Post
    What formula can i use to pick random arrays instead of ++ or -- that go up one or down one?
    If I'm correctly understanding your question, then you can use:

    int index = (int)(Math.random() * 5);

    Normally, Math.random() will select random numbers from 0 to as close to 1 as possible but not exactly 1, such as 0.9999999. When you explicitly cast it to an integer, the compiler would round to either 0 or 1. Multiplying it by some number enables you to specify the range of values that will be randomly selected from. Afterward, you can increase or decrease the value by 1 using ++ or --. You should be careful though because you may run into ArrayIndexOutOfBoundsException.

    Ideally, if you just wanted to get values out of your array in the order they were entered, consider using a for-loop, such as:

    for(int i = 0; i < myArray.length; i++) {
        // some code
    }

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: I dont know what to call this tittle.

    .

Similar Threads

  1. call by value and call by reference
    By Appu14 in forum The Cafe
    Replies: 1
    Last Post: September 2nd, 2012, 07:58 AM
  2. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 10:31 PM
  3. Call by Reference & Call by Value
    By Lokesh in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2011, 01:19 PM
  4. i'm getting this error, dont know why? please help
    By amr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 17th, 2010, 06:14 AM
  5. I dont get it....please help
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 30th, 2010, 04:16 AM