440,130 Members | 2,100 Online
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

# random numbers without using java funtions

 P: 53 i need a formula to generate 100 random numbers 1-20 any suggestions? Nov 15 '07 #1
4 Replies

 Expert 2.5K+ P: 3,652 Have a look at the Random.java class (Google "Random.java" and follow the first result). Nov 15 '07 #2

 P: 5 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.random()*(20)+1); System.out.println("Random number #"+xx+" = "+rnd); } Nov 15 '07 #3

 P: 53 we cant use math.random Nov 15 '07 #4

 Expert 100+ P: 849 As Ganon said, google the Random class (requires import java.util.Random;) and if you're having trouble understanding it, come back and we'll help you then. Nov 15 '07 #5