Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default Math.random Question

    I'm trying to figure out how to create a short program that accesses a separate file with a list of names and randomly prints one of the names in that file. For instance the file I would be accessing would look something like:

    Name 1
    Name 2
    Name 3
    etc

    How exactly might I go about using math.random to reference what number it generates with the name in the list in the other file? Any help would be much appreciated, and if you need any clarification just let me know. Thanks in advance!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    If you want to use Math.random, you'll end up with a double from 0.0 <= x < 1.0, so you'll then have to multiply that by the TOTAL amount of entries, then convert it to an int.

    Probably the easiest way to go about this is to read all lines of the file into a String array, then select a random number between 0 <= x < array.length. If you want to instead use the Random class (which can be used to generate integers in a range), have a look here.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    First, have you read the API doc for the Math.random class?
    There is another class that might be more useful for you: Random. It has a better choice of methods for obtaining a random number.

    If you MUST use the Math method, then you'll need to map the value it returns to the range of values you need for your program.

    EDIT: I feel like an echo.

  4. #4
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    lol... ok, ill check out the random class instead. seems like a lot less work. thanks for the help!

  5. #5
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post

    Probably the easiest way to go about this is to read all lines of the file into a String array, then select a random number between 0 <= x < array.length
    this is the part that im not exactly sure how to go about. can the file im reading from just be a standard txt file or does it have to be a java file?

    and how do i go about reading the strings into an array from the separate file?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    You'll want to use a standard text file for input.
    Search the forum for FileReader or BufferedReader for code samples on how to read a file.

  7. #7
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    So if I have something like this:

    public class random
    {
    public static void main (String[] args) throws IOException
    {
    Random choose = new Random();
    FileReader fr = new FileReader("names.txt");
    }
    }

    I'll need to somehow put "fr" into an array at this point? Not sure how to do that.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    To see how to use a class and its methods, you need to read the API doc.
    Java has many classes to do I/O and they are very confusing to the newbee for a long time.
    What you need to do is "wrap" the FileReader in another class: BufferedReader and use that classes methods to read the lines from the file that you can put in your array. Wrapping is the passing the object for one type of class to the constructor of another class that has more methods for what you want to do.

    Again do a Search for BufferedReader to get sample code.

  9. #9
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    ok, ok... we're making some progress. I searched for the BufferedReader and actually made some progress! Here is what i Have so far:

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

    public class random
    {
    public static void main (String[] args) throws IOException
    {
    Random choose = new Random();

    BufferedReader in = new BufferedReader(new FileReader("names.txt"));
    String str;
    while ((str = in.readLine()) != null)
    {
    System.out.println(str);
    }
    in.close();
    }
    }


    Right now it is printing every single name on my list, now i Just need it to print one name at random every time I run the program. Any suggestions?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    When do you know the total number of names? At that point you could use the Random class to give you a number from 0 to the number of names-1.
    The names would need to be stored in an array to be available.
    This gets to more complexity because when you define an array you must give it a size. That size must be larger than or eqaul to the number of names you read. There are classes that will handle the problem of overflow. See ArrayList for example.

    If the number of names is known before you start reading them (unlikely and poor practice)
    you could generate the random number before reading the names, read the names and count them and print when you get the one at the random number.

  11. #11
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    couldn't i just do something like mentioned before like:

    for(int i=0; i<args.length; i++)

    to get the length of the array?

    I have this so far:

    public class random
    {
    public static void main (String[] args) throws IOException
    {
    Random choose = new Random();

    BufferedReader in = new BufferedReader(new FileReader("names.txt"));
    String str;

    for(int i=0; i<args.length; i++)
    {
    String n=choose.nextString();
    System.out.printf("%s\n", n);
    }
    }
    }


    Getting an error on the line in red. I'm sure I'm just doing something stupid... like I said... I'm a noob :o

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    When you get an error, Please copy the full text of the message and paste it here.

    Does the Random class( the choose variable) have a nextString method?

    Where did the nextString method come from? Why did you use it?

  13. #13
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    I don't know what the hell I'm doing. This is really frustrating. I really need this to just print out 1 name instead of all of them and I can't figure it out :mad:

    public class random
    {
    public static void main (String[] args) throws IOException
    {
    Random choose = new Random();

    BufferedReader in = new BufferedReader(new FileReader("names.txt"));
    String str;
    while ((str = in.readLine()) != null)
    {
    System.out.println(str);
    }
    in.close();
    }
    }

  14. #14
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    i realize im not using the Random "choose" that I created, but I don't know how.

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Instead of printing out the lines, you need to add them to an array instead. Have you worked with arrays before?

    Using Random is detailed in the first link I provided:
    Quote Originally Posted by Leepoint.net
    int i = r.nextInt(int n) Returns random int >= 0 and < n.
    (Where r is a Random object; in this case it would be choose instead.)
    (Also you won't need this until the array I mentioned above is set up.)

  16. #16
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    i guess im not sure how to set up the array. how do you get the lines of text from the other file into an array?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    We'll take it one step at a time
    Do you know how to define an array of type String? Or of any type?

  18. #18
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    String[] stringname = new String[parameters];

    something like that?

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Yes as long as the parameters is an int and is at least as large as the most names you expect to get.
    Next you need to add the names as they are read to the array. Your will need an index to the array that starts at 0 and is incremented by 1 each time a string is added. Something like this:
    stringname[ix++] = str; // add a String to the array and increment the index (ix)

    ix will have to be defined, and assigned an initial value of 0 outside of your loop

  20. #20
    noble is offline Member
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Default

    Ok, it looks like I have everything figured out and it's working properly, but when I compile, I get the message:

    Note: random.java uses unchecked or unsafe operations.
    Note: Recompile w/ -Xlint:unchecked for details.


    The thing is, it compiles fine and lets me run the program anyway. Is this something that I should be worried about or something that can be easily fixed?

Page 1 of 2 12 LastLast

Similar Threads

  1. Math.random not zero
    By rsvr in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 01:57 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 01:26 AM
  3. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 09:28 AM
  4. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 09:55 PM
  5. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM

Posting Permissions

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