Generating Random Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enrique21
    New Member
    • Aug 2007
    • 1

    #1

    Generating Random Numbers

    Im trying to generate random numbers using the statement:

    Random r = new Random(2);
    for (int i=1;i<=18;i++)

    but im having errors in my results here is my entire code so far:

    import java.util.*;
    public class Random4
    {
    public static void main(String []args)
    {
    Random r = new Random(2);
    for (int i=1;i<=18;i++)

    {
    int n=r.nextInt(19) ;

    switch(n)
    {
    case 1:
    System.out.prin tln("n is a multiple of 3");
    break;
    case 2:
    System.out.prin tln("n is a multiple of 4");
    break;
    case 3:
    System.out.prin tln("n is a multiple of 5");
    break;
    default:
    System.out.prin tln("Chill dude");
    break;
    }

    System.out.prin tln(n);




    }
    }
    }

    Whats wrong with it?
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Wrong area moving you over to java area.

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Originally posted by enrique21
      Im trying to generate random numbers using the statement:

      Random r = new Random(2);
      for (int i=1;i<=18;i++)

      but im having errors in my results here is my entire code so far:

      [code=java]
      import java.util.*;
      public class Random4
      {
      public static void main(String []args)
      {
      Random r = new Random(2);
      for (int i=1;i<=18;i++)

      {
      int n=r.nextInt(19) ;

      switch(n)
      {
      case 1:
      System.out.prin tln("n is a multiple of 3");
      break;
      case 2:
      System.out.prin tln("n is a multiple of 4");
      break;
      case 3:
      System.out.prin tln("n is a multiple of 5");
      break;
      default:
      System.out.prin tln("Chill dude");
      break;
      }

      System.out.prin tln(n);




      }
      }
      }[/code]

      Whats wrong with it?
      Hello, and welcome to TSDN. First let's go over a few basics that will help you get the help you need:

      1. Use code tags please. See how right above me the code looks a little nicer, well when you post look to the right to a box that says "REPLY GUIDELINES" and it will explain to you how to use code tags.

      2. When you mention there are errors, it's helpful to tell us what those errors are, because often this is the biggest clue to figure out what's wrong with the program.

      On a last note, I'm not 100% sure but I don't know why you're setting the "seed" of your random variable to be 2 when you do "Random r = new Random(2)" (look at the documentation of the random class here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html ).

      That might be your issue, but then again I don't know what you want and what is showing up.

      Good luck,
      -blazed

      Comment

      Working...