Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 25th, 2007, 12:49 AM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generating random numbers


I am currently taking a Java based computer science paper at University and am a beginner.
My current assignment is writing a share trading program where users can decide how many of each type of share they would like to buy/sell and there are 5 time periods.
The value of each share (there are 3 types) at the start is a random value of either 5,6,7,8 or 9. In each new time period, the value of each type of share should either increase by 1, decrease by 1 or remain unchanged, and the change which occurs is random. So I have to generate a random number of -1, 0 or 1 to add to the values from the previous period.
So finally, my question. I know how to use Math.random to get 0 or 1, but how can I also randomly generate a negative number?

Thanks in advance for any help.

Reply With Quote
  #2 (permalink)  
Old March 25th, 2007, 11:26 PM
Authorized User
Join Date: Mar 2007
Location: , , USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

use the java.util.Random class, for example
java.util.Random rand = new java.util.Random();
// nextInt returns either 0, 1, or 2 and subtracting by 1 gives -1, 0, or 1
int addor = rand.nextInt(3) - 1;

Hope this helps

Reply With Quote
  #3 (permalink)  
Old March 26th, 2007, 12:22 AM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts


Thanks for the reply.
That is not actually something we have been taught or shown, but I have managed to work out what to do using Math.random

I've done:
int priceChange = (int)(Math.random()*3) - 1;
This generates a random integer from 0 to 2, then subtracts 1 from this to get an integer from -1 to 1.
Then just add that to the previous period share price to find the new share price
Thanks anyway for your reply.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Numbers Using MersenneTwister JoeDW C# 0 September 13th, 2006 11:10 AM
Random Numbers welshboy2005 C# 1 May 19th, 2005 06:01 AM
random numbers from/to database HammR PHP Databases 10 January 14th, 2005 12:08 PM
Generating random values gatsby VB How-To 3 August 4th, 2004 02:33 PM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM

All times are GMT -4. The time now is 08:21 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.