Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Number Generator

 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems storing floating point numbers from a random number generator. I can store it once but its when i have to iterate it that the problem comes in.



I have to cut that randomly generated number at the 3rd, 4th, 5th and 6th decimal. Then store it as I go in a variable and lastly have it add up to show the accumulated total through iterations. I have to do 10, 100, 1,000 and 10,000 reps. I am just trying to make the method and then pass the randomly generated number as an argument. I am just having a problem getting the total without actually showing all the work. Its like the variable will be out of scope I think.
 
Jeff Kendall
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your truncation should be outside of your main method body. I don't see where you are totaling your numbers and if you are trying to keep a running total your function should probably be returning a value instead of being void.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought void was just to let me make the method ?_?. Wait wait wait...so I need something like



Should it be like that? But I thought you could only return one thing. But I need to return the sums of the 3th, 4th, 5th and 6th. Like as it accumulates the 3th decimal numbers should be adding with the newly found 3th decimal and the 4th should do the same and so on.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only return one thing. But, that thing could be an array, or an object that is a collection of other things...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic