• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does either of these randomly generate both 0 and 1?  RSS feed

 
Jb Kendall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still quite new to Java and I had a quiz that asked "which code is best for randomly generating integer 0 or 1".
Haven't gotten my grade back yet but in reading these during the test I didn't think any of these would kick out
both of those numbers, but there was no "none of the above" option on the test.

This is exactly how it appeared on the test:
A) (int)Math.random() + 1
B) (int)(Math.random() + 0.2)
C) (int)Math.random()
D) (int)(Math.random() + 0.8)
E) (int)(Math.random() + 0.5)

I've tried all of them in the cs lab 100 times each and none of them generated both numbers. 'A' kicked out 1 every
time. 'B,D and E' kicked out 0.2, 0.8 and 0.5 respectively each time, and 'C' kicked out 0. Did I just not run them
enough times for the result to change or am I right in thinking there's a glitch on the test?

Thanks in advance.
 
Jb Kendall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind folks. I figured it out. Thanks anyway though.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jb Kendall wrote: . . . 'B,D and E' kicked out 0.2, 0.8 and 0.5 respectively each time, . . . .
No, they didn't. Not if you copied them correctly, that is.

The correct answer is, ”None of the above is best.” This is the best way to get a pseudorandom integer between 1 and 0:-
There is one of your bits of code which will produce 0 and 1 with equal frequencies.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jb Kendall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, they didn't. Not if you copied them correctly, that is.


Yes, it didn't take long after my first post for me to figure out that I was doing it completely wrong and that "+ 0.5" was the answer they were looking for. I figured the test questions were likely supplied by the textbook publisher (and therefore less likely to be wrong than I am). Also had a typo in the first post. I ran them 10 times and certainly not 100 times each, although it may as well have been 1,000 times each since wrong is wrong. I still have quite a ways to go before I really know what I'm doing. Maybe I'll back off the posting until I learn a bit more. Thanks anyway though.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:(...) None of the above is best. This is the best way to get a pseudorandom integer between 1 and 0 (...)

Hmm... since you put it in a way that leaves no doubt, can you explain in what way your method is 'best'?
(and why it is better than method E?)
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jb Kendall wrote:Maybe I'll back off the posting until I learn a bit more. Thanks anyway though.


Don't worry about that, just read what it says up next to the moose logo!
 
Jb Kendall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Don't worry about that, just read what it says up next to the moose logo!


Point taken
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are not risking getting the () in the wrong place, in which case E would return 0.5.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!