Results 1 to 15 of 15
  1. #1
    person287 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Random Number Generator with User Input

    Hi,
    I'm new to Java and I'm making a really simple random number generator, but I also want for the user to be able to choose the maximum number e.g. Random Number between 1-100, but the problem is I'm not sure of how to say it's an integer field, and I know that java is probably expecting that it'd be like 3 * hello which wouldn't work. Currently I've got the code;

    Java Code:
    import java.util.Scanner;
    
    public class ChoiceofRandomNumber
    {
        public static void main(String[] args)
        {
            Scanner r = new Scanner(System.in);
            System.out.println("What's the Maximum Number you Want to be Possible?");
            String name = r.nextLine();
            System.out.println((int) (Math.random() * r) +1);
        }
    }
    I'm guessing that this'll be a really easy fix but I'm not exactly sure where to go. Thanks for your help.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Java Code:
    int myInt = r.nextInt();
    Is that what you're trying to do? Make a scanner input that only accepts integers?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    person287 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Yeah that's exactly it! Thanks for the quick response.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok well 11 minutes isn't really slow, you could have found this out even faster by using Google. What I like to do, and actually how I found what you were looking for was by using Google.

    I typed in "Scanner class java 6 api" and this is the first link that showed up:
    Scanner (Java Platform SE 6)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Even better: download the entire API documentation from this link. It isn't much (mega-byte size speaking) but it's valuable stuff and having it all available (even off-line) takes away all those lame excuses where people simply guess, try and hope for the best. b.t.w. the download link can be found in the left margin, down the page.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @JosAH, good idea. I can't do that right now, but is there a search feature in the offline version?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dark View Post
    @JosAH, good idea. I can't do that right now, but is there a search feature in the offline version?
    afaik, the off-line version is identical to the on-line version (only faster, depening on your connection speed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    person287 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Downloading now (56.4MB) but the server seems to be quite slow. Thanks again.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok, thanks JosAH. I will keep that in mind for future reference.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    djscorpio99's Avatar
    djscorpio99 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    class Random1
    {
    public static void main(String args[])
    {
    Random ar=new Random();
    Scanner read=new Scanner(System.in);
    int n;
    System.out.println("Enter min value");
    int min=read.nextInt();
    System.out.println("Enter max value");
    int max=read.nextInt();
    if(max==min)
        System.out.println("Random number between "+min+ "-" +max+" is "+max);
    do
    {
    n=ar.nextInt(max);
    if(n>=min)
    System.out.println("Random number between "+min+ "-" +max+" is "+n);
    }
    while(n<min);
    }
    }
    Why dont you look at the above code.I am also new to JAVA and like to wite programs.Though i know it's bit complex code which i have written but i have just given a try.Pls do comment about it.
    Last edited by DarrylBurke; 07-09-2011 at 08:31 AM. Reason: Removed annoying all-bold formatting

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't know if you are looking for help or spoon-feeding, if the former, please make your own thread, otherwise, don't spoonfeed! Also, if you do post code, use code tags, not quote tags

    [code]
    YOUR CODE HERE
    [/code]

  12. #12
    djscorpio99's Avatar
    djscorpio99 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Hey , i am new to this forum ,I just joined today so was not knowing it and yes i am not seekin for any help ,i was just helping the thread owner to have look at the code whether it was this ,he was looking for and yes next time will use that code tag
    Last edited by DarrylBurke; 07-09-2011 at 08:31 AM. Reason: Removed annoying all-bold formatting

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by djscorpio99 View Post
    Java Code:
    import java.util.*;
    class Random1
    {
    public static void main(String args[])
    {
    Random ar=new Random();
    Scanner read=new Scanner(System.in);
    int n;
    System.out.println("Enter min value");
    int min=read.nextInt();
    System.out.println("Enter max value");
    int max=read.nextInt();
    if(max==min)
        System.out.println("Random number between "+min+ "-" +max+" is "+max);
    do
    {
    n=ar.nextInt(max);
    if(n>=min)
    System.out.println("Random number between "+min+ "-" +max+" is "+n);
    }
    while(n<min);
    }
    }
    Why dont you look at the above code.I am also new to JAVA and like to wite programs.Though i know it's bit complex code which i have written but i have just given a try.Pls do comment about it.
    That is a very inefficient solution. On average it'll take max/(max-min) iterations (for min < max) to find an appropriate pseudo random number.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That's fine to be new, but it will help the user if you just tell him what to use and let him do the work. For instance, on this Problem he will probably want to use Scanner, Random, and a loop, which can all be easily found in the API.

    Other than that, have fun here and don't get offended if people correct you.

    If you want to find a range you will want to find a number from ((max-min)+1)+min.

    If min is 20, max is 40, you would find numbers in the range 0-20(40-20 + 1) and add 20 to it.

    Java Code:
    nextInt(21); //generates numbers from 0(inclusive) to 21(exclusive)
    Last edited by sunde887; 07-09-2011 at 09:09 AM.

  15. #15
    djscorpio99's Avatar
    djscorpio99 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Hey JoSah, thanks for pointing out the inefficient complexity , i would take care of it in future for sure

    &

    @sunde887 ,thanks for yours suggestion too.Now no offence now onwards

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 12:37 PM
  2. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  3. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  4. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  5. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •