Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Chris Ward
Chris Ward
12,129 Points

How would you write this in Java?

Ok, as we all know, pseudo-random number generators are just that...pseudo. Their only guarantee for randomness applies to the law of large numbers which you might remember from a discrete math course :-).

Anyway, thinking about this problem in some detail, I came up with an interesting approach (although it is pseudo-code itself):

/*BEGIN (NOT C or C++, though some of the functions might be!)*/
double total = 0.0;
unsigned int selections = 0;
srand(time(NULL));
total += rand() & 255;
selections++;

/*THIS IS WHERE IT GETS WEIRD, AN IMPERATIVE-SQL-LANGUAGE-MARRIAGE*/
do { unsigned int next[8] do next[n = 0..7] = rand() & (unsigned int) (pow(2, n+1) - 1) select next[i = 0..7] where ((total + next[i]) / (selections + 1)) closest to (127.5) as theNextValue; total += theNextValue; selections++; } until (total / selections >= 127.0 && total / selections <= 128.0);

/*OK, STRANGE HYBRID CODE HAS ENDED!*/

Now, my question....how would you code this in Java?

1 Answer

Chris Ward
Chris Ward
12,129 Points

And thanks to the mods for fixing this post. I'm used to using the [code] tag which doesn't work here, but I've figured out how to do it now.