Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

method for randomly selecting boolean value

Thread view: 
Anand - 09 Mar 2007 22:27 GMT
hi  plz tell me how to code a method that will randomly pick boolean
value 0 or 1
Daniel Pitts - 09 Mar 2007 22:58 GMT
> hi  plz tell me how to code a method that will randomly pick boolean
> value 0 or 1

<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html>
Ashoka! - 10 Mar 2007 04:12 GMT
On Mar 10, 3:58 am, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:

> > hi  plz tell me how to code a method that will randomly pick boolean
> > value 0 or 1
>
> <http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html>

Generate a random integer by using the random class <http://
java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html> and then use
int bValue = randomInt % 2;
to get a o or 1 value.

regards
Usman Ismail
Lew - 10 Mar 2007 04:30 GMT
> On Mar 10, 3:58 am, "Daniel Pitts" <googlegrou...@coloraura.com>
> wrote:
[quoted text clipped - 7 lines]
> int bValue = randomInt % 2;
> to get a o or 1 value.

Didn't the OP ask for boolean results?

-- Lew
Patricia Shanahan - 10 Mar 2007 04:34 GMT
> On Mar 10, 3:58 am, "Daniel Pitts" <googlegrou...@coloraura.com>
> wrote:
[quoted text clipped - 7 lines]
> int bValue = randomInt % 2;
> to get a o or 1 value.

If {0,1} is really wanted, why not use myRandom.nextInt(2)? Or if a
boolean is wanted, myRandom.nextBoolean()?

The first step, of course, is for the OP to decide whether the values
should be boolean or {0,1}.

Patricia
Alex Hunsley - 10 Mar 2007 21:48 GMT
>> On Mar 10, 3:58 am, "Daniel Pitts" <googlegrou...@coloraura.com>
>> wrote:
[quoted text clipped - 10 lines]
> If {0,1} is really wanted, why not use myRandom.nextInt(2)? Or if a
> boolean is wanted, myRandom.nextBoolean()?

In fact, nextInt(2) is much preferred to the modulus way of doing things
(i.e. randomInt % 2), since the mod method tends to give less random
results.
And if crypto grade (pseudo) randomness is required, used SecureRandom
or similar.
http://java.sun.com/j2se/1.4.2/docs/api/java/security/SecureRandom.html

> The first step, of course, is for the OP to decide whether the values
> should be boolean or {0,1}.
>
> Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.