There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: How do I display ten random numbers in Java?


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Statler's Avatar
Member with 64 posts.
 
Join Date: Mar 2006
Location: Ontario, Canada
Experience: Intermediate
04-Mar-2007, 02:29 PM #1
Solved: How do I display ten random numbers in Java?
Yes. I am a beginner, and so I was hoping if someone knew how to display 10 random real numbers between 0 and 10 in Java. The problem I am having is displaying 10 different numbers generated from one Math.random statement. This is what I have so far:

public class RandomNumber {

public static void main ( String[] args ) {

double number;

number = Math.random() * 11;

System.out.println ("Here are ten random real numbers between 0 and 10:");

System.out.println (number);
}
}
Chicon's Avatar
Computer Specs
Distinguished Member with 6,602 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
04-Mar-2007, 05:04 PM #2
Just try this :

Code:

Random rnd = new Random();      
double number = rnd.nextDouble() * 11.0;
Statler's Avatar
Member with 64 posts.
 
Join Date: Mar 2006
Location: Ontario, Canada
Experience: Intermediate
05-Mar-2007, 11:43 AM #3
I tried that and I get an error:

Code:
public class RandomNumber {
    
	public static void main ( String[] args ) {
    	
		Random rnd = new Random();      
		double number = rnd.nextDouble() * 11.0;

		System.out.println (number);
    }
}
I just really want to know how to display 10 random numbers from that one single random statement. An example output would be:

5
3
5
2
7
6
7
1
9
5
Chicon's Avatar
Computer Specs
Distinguished Member with 6,602 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
05-Mar-2007, 12:23 PM #4
You forgot the import of the Random class :

Code:

import java.util.Random;

public class RandomNumber {
    
    public static void main(String[] args) {
        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(rnd.nextInt(10) + 1);
        }
    }
}

Statler's Avatar
Member with 64 posts.
 
Join Date: Mar 2006
Location: Ontario, Canada
Experience: Intermediate
05-Mar-2007, 10:17 PM #5
Okay, thanks alot for the help!
Chicon's Avatar
Computer Specs
Distinguished Member with 6,602 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
06-Mar-2007, 12:56 AM #6
You're welcome !
Statler's Avatar
Member with 64 posts.
 
Join Date: Mar 2006
Location: Ontario, Canada
Experience: Intermediate
06-Mar-2007, 10:21 AM #7
Darn it. One more thing I forgot to say. Can anyone tell me how you would display random real numbers between 20 and 30 because rnd.nextDouble() seems to only work between 0 and another number.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,602 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
06-Mar-2007, 03:01 PM #8
Simply this way :

Code:

Random rnd = new Random();
double val = rnd.nextDouble() * 10.0; // the command will produce a real number betweem 0 and 10 // as rnd.nextDouble() returns a value between 0 and 1
val = val + 20.0;                     // val will contain a real number between 20 and 30 // also, notice the way to write a real number : a decimal point is mandatory
                                      // to avoid the confusion with an integer 

Last edited by Chicon : 06-Mar-2007 05:40 PM.
Statler's Avatar
Member with 64 posts.
 
Join Date: Mar 2006
Location: Ontario, Canada
Experience: Intermediate
06-Mar-2007, 06:08 PM #9
Thanks again.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:45 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.