how to generate random number between -n and n

Discussion in 'Java' started by Ricardo, Jul 15, 2006.

  1. Ricardo

    Ricardo Guest

    Hi wizards , I need to know how to generate random numbers between -n
    and n with matlab , n is a integer .
    Thanks in advance .
     
    Ricardo, Jul 15, 2006
    #1
    1. Advertising

  2. Ricardo wrote:
    > Hi wizards , I need to know how to generate random numbers between -n
    > and n with matlab , n is a integer .
    > Thanks in advance .


    If n>=1 or n<=-1 then java.lang.Math.random() will generate a pseudo
    random number between -n and n. (But not from a uniform (-n,n)
    distribution.)

    If n=0, then it is degenerate. Just choose 0.
     
    Furious George, Jul 15, 2006
    #2
    1. Advertising

  3. Ricardo

    Red Orchid Guest

    "Ricardo" <> wrote or quoted in
    Message-ID: <>:

    > Hi wizards , I need to know how to generate random numbers between -n
    > and n with matlab , n is a integer .
    >


    For x,
    if 0 < x < 2n,

    0 - n < x < 2n - n
    and then,
    -n < x < n


    Besides,
    'Random.nextInt(2*n)' produces '0 <= x < 2n'.

    Java documentation says about 'Random.nextInt(int n)'

    "... All n possible int values are produced with (approximately)
    equal probability ... "

    Therefore,
    if 'Random.nextInt(2*n)' were a perfect source,

    'Random.nextInt(2*n) - n' would produce
    -n < x < n
    with perfect uniformity.
     
    Red Orchid, Jul 15, 2006
    #3
  4. Ricardo

    Chris Uppal Guest

    Ricardo wrote:

    > Hi wizards , I need to know how to generate random numbers between -n
    > and n with matlab , n is a integer .


    "with matlab" -- I think you should ask in a different forum.
    comp.soft-sys.matlab seems quite active at a casual glance.

    -- chris
     
    Chris Uppal, Jul 15, 2006
    #4
  5. Ricardo

    Red Orchid Guest

    "Red Orchid" <> wrote or quoted in
    Message-ID: <e9adp3$m7k$>:

    >
    > 'Random.nextInt(2*n) - n' would produce
    > -n < x < n
    > with perfect uniformity.



    Correct.

    -n <= x < n


    If '-n < x < n' is required,

    <code>
    Random ran = new Random();
    ....
    int n = ...
    int value = ran.nextInt(n);
    int sign = ran.nextInt(2);

    ..... = (sign == 0) ? value : -value;

    </code>
     
    Red Orchid, Jul 15, 2006
    #5
  6. Ricardo

    Red Orchid Guest

    "Red Orchid" <> wrote or quoted in
    Message-ID: <e9ai1o$noa$>:

    > If '-n < x < n' is required,
    >
    > <code>
    > Random ran = new Random();
    > ...
    > int n = ...
    > int value = ran.nextInt(n);
    > int sign = ran.nextInt(2);
    >
    > .... = (sign == 0) ? value : -value;
    >
    > </code>



    Sorry.

    The above code do not have uniformity because 0 can
    be chosen as +0 and -0.


    If -n < x < n, the solution will be

    Random.nextInt( 2 * (n -1) + 1 ) - ( n - 1 );


    Because, ..

    'Random.nextInt( 2 * (n -1) + 1 ) ' produces

    0 <= x < 2 * (n -1) + 1

    Then,

    0 - (n -1) <= x < 2 * (n - 1) + 1 - (n - 1)

    -(n - 1) <= x < (n - 1) + 1

    -(n - 1) <= x < n

    -n < x < n (here, n is integer).
     
    Red Orchid, Jul 15, 2006
    #6
  7. Ricardo

    Ricardo Guest

    n is a positive integer , I need to know it in Java.


    Ricardo wrote:
    > Hi wizards , I need to know how to generate random numbers between -n
    > and n with matlab , n is a integer .
    > Thanks in advance .
     
    Ricardo, Jul 15, 2006
    #7
  8. Ricardo wrote:
    > n is a positive integer , I need to know it in Java.
    >
    >
    > Ricardo wrote:
    >> Hi wizards , I need to know how to generate random numbers between -n
    >> and n with matlab , n is a integer .
    >> Thanks in advance .

    >


    Do you want an integer or a double? Are the range limits inclusive, or
    exclusive?

    Generally, to do this sort of thing in Java you use an instance of
    java.util.Random.

    Patricia
     
    Patricia Shanahan, Jul 16, 2006
    #8
  9. Ricardo wrote:
    > Ricardo wrote:
    >> Hi wizards , I need to know how to generate random numbers between -n
    >> and n with matlab , n is a integer .
    >> Thanks in advance .

    >
    > n is a positive integer , I need to know it in Java.
    >


    You need to understand the math.

    You wish to generate random scalars in a predetermined range
    between -k and k such that
    -k <= x < k

    now, if you add k to that you get
    0 <= k+x < 2k

    use java.util.Random to generate an integer (k+x) < 2k
    then, to get the desired value x just subtract k.
     
    Thomas Schodt, Jul 16, 2006
    #9
  10. Ricardo

    Ricardo Guest

    Thanks I understand this issue


    Thomas Schodt wrote:
    > Ricardo wrote:
    > > Ricardo wrote:
    > >> Hi wizards , I need to know how to generate random numbers between -n
    > >> and n with matlab , n is a integer .
    > >> Thanks in advance .

    > >
    > > n is a positive integer , I need to know it in Java.
    > >

    >
    > You need to understand the math.
    >
    > You wish to generate random scalars in a predetermined range
    > between -k and k such that
    > -k <= x < k
    >
    > now, if you add k to that you get
    > 0 <= k+x < 2k
    >
    > use java.util.Random to generate an integer (k+x) < 2k
    > then, to get the desired value x just subtract k.
     
    Ricardo, Jul 16, 2006
    #10
    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:
    796
    Gabriel Genellina
    Apr 20, 2008
  2. Sanchit
    Replies:
    20
    Views:
    18,868
    Richard Bos
    Oct 9, 2008
  3. sintral
    Replies:
    9
    Views:
    4,374
    Ben Bacarisse
    Dec 7, 2008
  4. Alex Untitled
    Replies:
    11
    Views:
    678
    Giampiero Zanchi
    Nov 16, 2009
  5. VK
    Replies:
    15
    Views:
    1,279
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page