random numbers

Discussion in 'Java' started by Ams, Feb 23, 2005.

  1. Ams

    Ams Guest

    hi,
    I am writing a method to invoke random numbers. I need the numbers to
    be in the range 0-1000 but the numbers that i have created are huge
    with around 7 digits. Does anyone no how or if its even possible to do
    this??
    Would greatly appreciate any help...
     
    Ams, Feb 23, 2005
    #1
    1. Advertising

  2. Ams wrote:
    > hi,
    > I am writing a method to invoke random numbers. I need the numbers to
    > be in the range 0-1000 but the numbers that i have created are huge
    > with around 7 digits. Does anyone no how or if its even possible to do
    > this??
    > Would greatly appreciate any help...


    Look at the nextInt(int n) method, this returns a random int between 0
    and n-1.

    HTH
    ==============================================================
    TechBookReport Java: http://www.techbookreport.com/JavaIndex.html
     
    TechBookReport, Feb 23, 2005
    #2
    1. Advertising

  3. Ams

    Malte Guest

    Ams wrote:
    > hi,
    > I am writing a method to invoke random numbers. I need the numbers to
    > be in the range 0-1000 but the numbers that i have created are huge
    > with around 7 digits. Does anyone no how or if its even possible to do
    > this??
    > Would greatly appreciate any help...


    Hmm, it's been a while, but something like this:

    int bigRandom;
    int myRandom;

    myRandom = (bigRandom%1000) + 1;

    ?
     
    Malte, Feb 23, 2005
    #3
  4. Ams

    Alun Harford Guest

    "Ams" <> wrote in message
    news:...
    > hi,
    > I am writing a method to invoke random numbers. I need the numbers to
    > be in the range 0-1000 but the numbers that i have created are huge
    > with around 7 digits. Does anyone no how or if its even possible to do
    > this??
    > Would greatly appreciate any help...


    java.util.Random random = new java.util.Random();
    int requiredRandomNumber = random.nextInt(1001);



    Alun Harford
     
    Alun Harford, Feb 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    795
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    678
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    229
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    264
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,277
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page