Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to generate random number between -n and n

Reply
Thread Tools

how to generate random number between -n and n

 
 
Ricardo
Guest
Posts: n/a
 
      07-15-2006
Hi wizards , I need to know how to generate random numbers between -n
and n with matlab , n is a integer .
Thanks in advance .

 
Reply With Quote
 
 
 
 
Furious George
Guest
Posts: n/a
 
      07-15-2006

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.

 
Reply With Quote
 
 
 
 
Red Orchid
Guest
Posts: n/a
 
      07-15-2006
"Ricardo" <(E-Mail Removed)> wrote or quoted in
Message-ID: <(E-Mail Removed) .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.








 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      07-15-2006
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


 
Reply With Quote
 
Red Orchid
Guest
Posts: n/a
 
      07-15-2006
"Red Orchid" <(E-Mail Removed)> wrote or quoted in
Message-ID: <e9adp3$m7k$(E-Mail Removed)>:

>
> '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>











 
Reply With Quote
 
Red Orchid
Guest
Posts: n/a
 
      07-15-2006
"Red Orchid" <(E-Mail Removed)> wrote or quoted in
Message-ID: <e9ai1o$noa$(E-Mail Removed)>:

> 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).

 
Reply With Quote
 
Ricardo
Guest
Posts: n/a
 
      07-15-2006

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 .


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-15-2006
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
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      07-16-2006
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.
 
Reply With Quote
 
Ricardo
Guest
Posts: n/a
 
      07-16-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
Random float between 0 and 1, then a second random float between 0and 1 - x sintral C++ 9 12-07-2008 02:23 AM
How to generate random number between two numbers Sanchit C Programming 20 10-09-2008 10:21 AM



Advertisments