Results 1 to 5 of 5

Thread: Want an array of Random numbers

  1. #1
    Join Date
    Nov 2009
    Posts
    347

    Want an array of Random numbers

    I am learning java programming, and want to develop a small program code to help me with my other codes. I am looking to develop a program that will give me random numbers which i can assign to array which is of user-defined size. Would appreciate any help from the techies in here.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Want an array of Random numbers

    If you want to take the input array size from the user, you need to use the JOptionPane().
    After you get the array size, make a new array of that size. Then what you need to do is use a loop to fill the array with random numbers, which can be generated by the Math.random(). Remember: Math.random() gives value between 0 and 1. For your purpose, you can multiply the above number by some number and floor it by the Math.floor() method. Like if you want the range from 0 to 10, multiply the random number by 10. In case you still face any problems, please feel free to post it
    Last edited by Modifier; 03-11-2009 at 02:19 PM.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Want an array of Random numbers

    I do know how to use the JOPane() and will try that Math.random() and Math.floor(). I have one more question, how do i increase the lower level of the limit.? that is if the lower limit is 0 and i want it to be 5? Thanks a lot!
    Last edited by InterNetWorKed; 03-11-2009 at 02:22 PM.

  4. #4
    Join Date
    Nov 2009
    Posts
    343

    Re: Want an array of Random numbers

    I am pretty new to Java. I was going through the post and I tried he Math.random() and Math.floor(), but i want to know what exactly does the Math.floor() method do..?? i mean i used it alright, but what does it do..???
    Last edited by Windowed; 03-11-2009 at 02:20 PM.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Want an array of Random numbers

    InterNetWorKed Just add the number to the floored number like
    Code:
    Math.floor(Math.random()*10)+5
    Windowed
    Flooring means rounding up.. like when u get a random number, it will be between 0 and1, and when you multiply it with some number it can result in a decimal number, but what is required is an integer.So the Math.floor() is used. remember: Math.floor() rounds just removes the decimals.. that is if number is 2.1 it will be rounded to 2, also 2.9 will be rounded to 2 as it is being Floored.
    In case of anything else please feel free to ask.

Similar Threads

  1. How to fill an array with random number in c++?
    By Juaquine in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 05:34 PM
  2. Random Numbers in C
    By Rob Dizzle in forum Software Development
    Replies: 5
    Last Post: 30-12-2009, 10:37 AM
  3. Function for two random numbers
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 12:39 PM
  4. Random numbers. Help
    By tetilda in forum Software Development
    Replies: 2
    Last Post: 15-09-2008, 05:52 PM
  5. Counting numbers and deleting array
    By SalVatore in forum Software Development
    Replies: 2
    Last Post: 25-08-2008, 01:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,693,966.56017 seconds with 17 queries