• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random numbers displayed a certain amount of times  RSS feed

 
Jon Hankssss
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what I have so far. I know my for loop is off, but I can't seem to figure out how to display a certain amount of random numbers between 1 to 99 based off how many the user wants total and per line.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jon!

It helps others if you format your code properly. Right now, the alignment and indentation is all out of whack making it harder to follow the logic.

When something doesn't work, it also helps to state specifically what kind of error you're getting. Did the code compile? Were you able to run the code but got errors? Or did you get unexpected results? It helps if you TellTheDetails (← click that link for more).

Looking at your code, I'm guessing it doesn't compile. Check out the JavaDocs for Math.random(), you'll see that the way you're using it doesn't match the way the API defines it.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.random() doesn't take any arguments. Also, Math.random() is old school and has been replaced by the Random class which has a bunch of different methods (see the Javadocs).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you need to generate random numbers within a range, like from 0-99, it's much easier to use a java.util.Random object instead.

Here are the JavaDocs for java.util.Random: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

The convention in Java for for-loop control is to start from 0 then use < limit as your loop condition.  When you use 1 as your starting index and <= limit for the loop condition, others will have to do a double take and make sure nothing out of the ordinary is happening. The 0 + lessThan convention is used mostly because indices to common data structures like arrays and lists are 0-based in Java, unlike in languages where indices are usually 1 based and it makes more sense to use 1 + lessThanOrEqualTo convention for the for-loop controls.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought that both the Math class and the Random class were introduced in the earliest versions of Java® and Math#random worked by using an instance of Random. So I agree with Junilu (I should have been quicker posting) that using a Random instance is better. You also avoid all sorts of strange arithmetic. I don't know whether this will help, but Java8 introduced this method, which produces an IntStream; you can use its toArray() method to create an array directly.You will unfortunately find it a bit awkward to print the numbers in twenties from a stream; I am sure somebody will think of a good way to do it though. Look on the ???s as a challenge to work out what to write there.

Everybody calls the class Random, but its output isn't truly random. At this stage however, none of us will ever notice the difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!