• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populating an array with random numbers  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Netbeans is playing with my head.

I'm trying to populate my array with random numbers between 1 & 6. I did it successfully & then started playing around trying to do my assignment (count the number of occurrences of each number), & I don't know what I did, but now it won't print out random numbers. All it will print out is zeroes. What's there is exactly what's in my book, except that I use * 10 rather than * 100 to get the random number, & the name of my array is different. I've compared what's here to what's in the book a zillion times, & I can't figure it out.



I formatted this first on Netbeans, so I hope the format is OK.

I realize that, at best, this will populate the list with numbers between 0 & 10, but at this point, I'd be happy to get back to that. What have I messed up? I guess I could have missed something all one zillion times I looked at it in the book, but I'm totally stumped.

Thank you!

 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better if you filled in the whole array with random numbers before printing it out. As it is, you fill in one of the array entries and then print out the whole array, and then fill in the second array entry and print out the whole array, and so on.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I moved some brackets around to try to do as you suggest, but since all I'm getting is zeroes no matter what I do, the order of actions really doesn't matter at this point, does it? I have to figure out how to get something besides 0 into the array.
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha... what I said is probably correct in its own way, but not relevant to your current problem which is this line of code:



Try putting in a print-statement after that and see if it's always zero.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like this:



That's still 0, but it's only 1 zero.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Stream. It will do all the hard work for you. What is more, you can copy the code which you will find on the web. No, that example is no good; I know who wrote it.
Try
int[] randomNumbers = new Random().ints(10L, 100, 200).toArray();
That will give you a ten‑element array containing numbers “randomly” chosen between 100 and 199. Obviously you can put different numbers in there. If you click on the ints link you find it creates an IntStream which does all the repetition for you.

That code won't work in Java7.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible, I need to stick with what we've done in class, although I'm reaching the point where I'm going to do what's easiest, even if I don't understand it at this point, because understanding is starting to take up way too much time. I know that I can do it with Math.random(), because it WAS working (although you'd probably tear out your hair if you saw how I was making it generate numbers between 1 & 6), but I've done something to mess it up & can't figure out what.

Which code won't work in Java 7?
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:You mean like this:
. . . .
No.

Another post by that chap who doesn't know what he is taking about. Read the whole thread if you have time. Note whether you have got into the position where…
Some Bozo once wrote:Putting the () in the wrong places around the cast will probably give you a pseudo‑random int between 0..0
Find the suggestion to use a Random object, which is much less error‑prone.
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:That's still 0, but it's only 1 zero.


Looks like in all the confusion you've lost track of what you were trying to do. Which was:

I'm trying to populate my array with random numbers between 1 & 6.


So, you're getting zero from that line of code which I highlighted. And "only 1 zero" is still in conflict with "between 1 & 6", isn't it? So you need to fix that line of code so it doesn't always return zero. I'd suggest leaving aside the code with all the loops for the time being and just write a small piece of code which produces one single random number between 1 and 6. Then incorporate that working code into your code with all the loops in it.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:. . . Which code won't work in Java 7?
int[] randomNumbers = new Random().ints(10L, 100, 200).toArray();
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Paul for getting at cross‑purposes with you.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a mess. OK, I'm saving what I have now before I go trying to do anything else with it.

I remember removing the parentheses. And even I know what happens when you ask for an integer between 0 & .1 & then multiply it by 10. My brain just doesn't interpret very well or fast.

Also, I finally realized that I had two lines doing the exact same thing, & I don't know what that was actually doing except not working. Obviously, you can't take two pieces out of the book & glue them together & get both results.

Thank you. I am definitely not giving up my day job.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the links I gave you yesterday, you should find an example with the () in the right place.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!