import java.util.*; class randomnum { public static void main(String arg[]){ int i = 20; Random r = new Random(); while(i>0){ System.out.println(r.nextInt()); i--; } } }
# java randomnum -1710192698 372834272 561813874 964075960 -543878181 -518949113 -201238297 562882756 -658754567 2144570707 -1321395011 -273056203 -234056158 -1293542699 -1337926200 265104174 1380461933 588544298 -736641255 1568232921
For a bounded number, you could use the %, or modulo operator, which gives you the remainder of a division operation. A random number r operated upon by modulo, r%10, for example, will not be greater than 9. Try adding %100 to the print statement:
System.out.println(r.nextInt()%100);
We might also want to make the numbers positive. The Java Math class has an absolute value method that makes any negative number positive:
int posNum = Math.abs(r.nextInt());
String lyric[] = {"I", "you", "love", "hurt", "sad", "rock", "do", "baby", "man", "car", "sex", "rockin", "smokin", "forever" };
Next we would need a way to
generate the index numbers, between 0 and lyric.length.
Random r = new Random();
int i = Math.abs(r.nextInt()%lyric.length);
Here, the things inside the Math.abs() parentheses will be evaluated prior to returning the number to the i variable.
while(i>0){ ... while(h>0){ ... } ... }
Several samples:
# !! java rocklyric smokin baby you do car. I (Guitar solo) rock ooo! (Guitar solo). sad forever sex smokin sex. sad forever ooo! you smokin. sad uh-huh I uh-huh ooo!. # !! java rocklyric sad sad do hurt man. (Guitar solo) hurt baby sex smokin. rock rockin hurt I rockin. sad sad smokin love smokin. hurt do sex sex do.
For Mon. Mar. 10, Improve upon the rock music generator, perhaps making it produce other genres of music when given input strings, then convert it into a text/html utility that is invoked from a .cgi script. You might use a form to invoke it so that the person on the other end chooses the genre of music that she/he would like to see generated. Note: You're free to work on other text-based Java projects too, if you have an idea that appeals to you more.Steps: