Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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

Hi,

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.


  #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
Default

use the java.util.Random class, for example
Code:
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;
http://java.sun.com/javase/6/docs/api/

Hope this helps

  #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
Default

Hi,

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.



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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.