• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

random number

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone know a slick way to create a random number between a negitive number and it's corosponding positive one?
i.e. gimme a random # between -5 and 5
thanks
-tyhooke
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this?


Henry
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A more generic solution may be:



The output:

 
tyhooke cologne
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these help. thank you both!
-Tyhooke
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about:


This gives you a random index (range of 0 to [array length - 1]) that will then give you the random contents (-5 to 5).
[ January 12, 2005: Message edited by: PJ Chin ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, but what happens when the user comes back and says, "Oh, now we need -10 to 10"? The hard-coded values method would need to be rewritten, where the general method would not. I always try to think "Reusability" when I write code, and part of that is not having any hard-coded values (except perhaps in the testing/driver program)...
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Bosch:
int random = r.nextInt( 2 * number ) - number;

Keep in mind that Random.nextInt(number) returns a value in the range [0, number), meaning 0, 1 ... number - 1. It will not return number itself. Thus the general solution should be changed to
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, good point! I'd whipped that solution up in haste and something about it just didn't feel right, but I didn't really have time to go back to it.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add more flexibility, you may want to write a method with the following signature:


This would return a random integer between min and max. I'll leave the implementation as the proverbial exercise for the reader. However, it shouldn't be difficult to modify Jeff's program above for this purpose.

Layne
 
Why is the word "abbreviation" so long? And this ad is so short?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic