how to generate random number between -n and n

R

Ricardo

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

Furious George

Ricardo said:
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.
 
R

Red Orchid

Message-ID: said:
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.
 
C

Chris Uppal

Ricardo said:
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
 
R

Red Orchid

Message-ID: said:
'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>
 
R

Red Orchid

Message-ID: said:
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).
 
P

Patricia Shanahan

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

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
 
T

Thomas Schodt

Ricardo said:
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.
 
R

Ricardo

Thanks I understand this issue


Thomas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top