Hi wizards , I need to know how to generate random numbers between -n
and n with matlab , n is a integer .
Thanks in advance .
Furious George - 15 Jul 2006 06:01 GMT
> 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.
Red Orchid - 15 Jul 2006 10:50 GMT
"Ricardo" <ricrio@gmail.com> wrote or quoted in
Message-ID: <1152921907.595376.304880@s13g2000cwa.googlegroups.com>:
> 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 - 15 Jul 2006 12:03 GMT
"Red Orchid" <windfollowcloud@yahoo.com> wrote or quoted in
Message-ID: <e9adp3$m7k$1@news2.kornet.net>:
> '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 - 15 Jul 2006 12:43 GMT
"Red Orchid" <windfollowcloud@yahoo.com> wrote or quoted in
Message-ID: <e9ai1o$noa$1@news2.kornet.net>:
> If '-n < x < n' is required,
>
[quoted text clipped - 8 lines]
>
> </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).
Chris Uppal - 15 Jul 2006 11:09 GMT
> 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
Ricardo - 15 Jul 2006 16:24 GMT
n is a positive integer , I need to know it in Java.
> Hi wizards , I need to know how to generate random numbers between -n
> and n with matlab , n is a integer .
> Thanks in advance .
Patricia Shanahan - 16 Jul 2006 00:45 GMT
> n is a positive integer , I need to know it in Java.
>
>> 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
Thomas Schodt - 16 Jul 2006 06:44 GMT
>> 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 - 16 Jul 2006 15:13 GMT
Thanks I understand this issue
> >> Hi wizards , I need to know how to generate random numbers between -n
> >> and n with matlab , n is a integer .
[quoted text clipped - 14 lines]
> use java.util.Random to generate an integer (k+x) < 2k
> then, to get the desired value x just subtract k.