File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Number Generator

 
Alan Green
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want the for loop to do?
 
Alan Green
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 688
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic