• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating 100 random numbers?  RSS feed

 
Jessie Soto
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an assignment in which I have to generate 100 random numbers, betwen 1 and 10, and store how many times the numbers are generated. I know how to generate a random number using Math.random, but I don't know to do it 100 times.
Any help?

Here is what I have so far.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Looking at your code, I see you already know what a for-loop is.

Now, you just have to make your program do whatever it needs to do inside the loop. In your current code, in line 13, you get one random number, and then you start doing things with that one number in a for-loop. Instead, you need to put getting the random number inside the loop, so that the computer gets a new random number 100 times, instead of just once.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Please post your code here. Many people are reluctant to go to pastebin, so you will lose out on potential replies.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also do not need all those ifs. There is a far simpler way to get into the array.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search about generating random numbers. You will find several threads from last week. This is a discussion about a similar problem, which I believe you will find helpful. You will see there are at least two ways to get random numbers in a range, and you will also find a pitfall mentioned.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!