# how to generate random number between -n and n

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

1. ### RicardoGuest

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

Ricardo, Jul 15, 2006

2. ### Furious GeorgeGuest

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

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

3. ### Red OrchidGuest

"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
4. ### Chris UppalGuest

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
5. ### Red OrchidGuest

"Red Orchid" <> wrote or quoted in

>
> '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
6. ### Red OrchidGuest

"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
7. ### RicardoGuest

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 .

Ricardo, Jul 15, 2006
8. ### Patricia ShanahanGuest

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 .

>

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
9. ### Thomas SchodtGuest

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 .

>
> 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
10. ### RicardoGuest

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