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.
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.
Which code won't work in Java 7?
Karen Guffey wrote:You mean like this:
. . . .
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…
Find the suggestion to use a Random object, which is much less error‑prone.
Some Bozo once wrote:Putting the () in the wrong places around the cast will probably give you a pseudo‑random int between 0..0
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.
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.