• Post Reply Bookmark Topic Watch Topic
  • New Topic

RANDOM NUMBERS  RSS feed

 
makhan gee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to generate random numbers from -0.5 to 0.5 using Math.random().
Thankzzzz in advance.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Well, java.util.Random has a method nextDouble() which returns a number between 0.0 and 1.0 . You can use that, applying simple arithmetic to change the range of the value. If you have trouble, show us what you've done so far and we can tell you where any problems might lie.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that as simple as taking the result of Math.random() and subtracting 0.5?

If you care about the exact boundaries, there might be a problem. Math.random() gives a number greater than or equal to 0.0 and less than 1.0. So Math.random() - 0.5 gives a number great than or equal to -0.5 and less than 0.5. Is that close enough, for your application?

Note that Java programmers usually use the java.util.Random class, rather than Math.random() method. To understand why, read the Java API documentation on both.
[ September 11, 2006: Message edited by: Peter Chase ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by makhan gee:
Hi,
I want to generate random numbers from -0.5 to 0.5 using Math.random().
Thankzzzz in advance.


Although your name policy doesn't match with Rancher's Names Policy.

I request to Moderators to notice that..


[ EJFH: Deleted code; this is clearly homework, let the guy do his own work, please. ]
[ September 11, 2006: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:

Although your name policy doesn't match with Rancher's Names Policy.


Can you tell us why? Sounds fine to me, but of course I don't speak any Indian languages.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


Can you tell us why? Sounds fine to me, but of course I don't speak any Indian languages.


Yes Offcourse I can tell you, why it is not a valid name.

makhan gee

Makhan -- Butter

Gee or Ghee -- Fat

These are the words used in Hindi very often(Indian Language).

I hope now you are clear on the name of makhan gee
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!