• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random Number Generator

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want the for loop to do?
 
Alan Green
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic