wood burning stoves 2.0*
The moose likes Java in General and the fly likes Random Number Generator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Random Number Generator" Watch "Random Number Generator" New topic
Author

Random Number Generator

Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
I am trying to write a program that prompts the user to enter a number between 1 to 1000.Randomly generating the users input between 1 to 1000. I am using the Random generator = new Random () method. I am having trouble implementing the for loop.Can someone help?

import java.io.*;
import java.util.Random;

class assmt2
{

public static void main(String[] args)throws IOException{
Random generator = new Random(1000);
int temp = 0;
System.out.println(" Enter Integers :"); //prompt user

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
int next=Integer.parseInt(keyRead.readLine());
temp = generator.nextInt();

// for(int i = 0; i<x; i++)

System.out.print(i);
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

What do you want the for loop to do?


[Jess in Action][AskingGoodQuestions]
Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
I was thinking I might need a loop, so when the user inputs a number the loop will hold that number. I was thinking it will breakout the loop when it has reach the users inputed number. Giving the random numbers between 1 to 1000. What do you suggest I do?
Thanx
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Joe suave:
I was thinking I might need a loop, so when the user inputs a number the loop will hold that number. I was thinking it will breakout the loop when it has reach the users inputed number. Giving the random numbers between 1 to 1000. What do you suggest I do?
Thanx

I'm sorry, I read this four or five times and I still can't figure out what you want this program to do.
How about this: can you show me a fictitious interactive session with this program? I.e., the prompts, what the user types, what gets printed. Then maybe I'd understand. I've only had one beer tonight but I'm tired so maybe it was enough to addle my brain!
Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
Lets say the user enters the number 12. I am trying to Random Generate 12 numbers between 1 to 1000.
Like this........Enter a Integer 12
Output 100 56 725 32 552 5 366 321 411 945 200 201
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Ah, OK, well, that's easy enough. You wrote in your original message

The loop control is basically correct; you want to change "x" to "next" so that the upper limit of the loop is the number the user typed in. Then, give the loop a body (a set of "{}" curly braces) and inside the body, do two things: call generator.nextInt() to get a new random number, and then print it.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
I've only had one beer tonight but I'm tired so maybe it was enough to addle my brain!

admit it! You are a light weight!!!
Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
I don't understand how the braces are going to be . Is it going to be a while statement?
class assmt2
{

public static void main(String[] args)throws IOException{
Random generator = new Random();
int temp;
System.out.println(" Enter Integers :"); //prompt user

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
int next=Integer.parseInt(keyRead.readLine());

for(int i=0; i<next; i++)
//braces
temp = generator.nextInt(1001);

System.out.print(next);
}
}
Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
Can someone help me
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
No need for a while loop.
All you have to do is to enclose the generator.nextInt() and the output statement (System.out.print) with in the for loop.

Probably u might need some formatting in the output (some space between each random number).
Hope that helps.


Mani
Quaerendo Invenietis
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
And a small advice. (Nothing new, but same as what Cindy
advised earlier.
While posting code, please use the CODE option to indent it properly.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Random Number Generator
 
Similar Threads
Unique Random Number Generator : Please suggest improvements to this code
GUI number guessing game program
Nedd Help On Random Nubber Generating
Trouble with variables...
Random numbers