random numbers without using java funtions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    random numbers without using java funtions

    i need a formula to generate 100 random numbers 1-20

    any suggestions?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Have a look at the Random.java class (Google "Random.jav a" and follow the first result).

    Comment

    • comando2929
      New Member
      • Nov 2007
      • 5

      #3
      I'm gonna asume that you understand for loops and can create the 100 numbers. To create a random number use Math.random() to get a number between 0 and 1. Then multiply by 20 because you need 20 different numbers. then add 1 to get results from 1 to 20.999 instead of 0-19.999. Then, asuming you want whole integer numbers, cast the resulting double to and int. There you go. Not too hard.


      for(int xx = 1; xx <= 100; xx++)
      {
      int rnd = (int)(Math.rand om()*(20)+1);
      System.out.prin tln("Random number #"+xx+" = "+rnd);
      }

      Comment

      • carlos123
        New Member
        • Sep 2007
        • 53

        #4
        we cant use math.random

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          As Ganon said, google the Random class (requires import java.util.Rando m;) and if you're having trouble understanding it, come back and we'll help you then.

          Comment

          Working...