954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

random

How can I generate a random boolean? Is there a method for this?

Thanks in advance

vex
Newbie Poster
22 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

I think you could do it like this, but I didn't test this.

public boolean randomBoolean()
	{
	int r = (int) Math.random() * 10;
	
	if (r < 5)
		return true;
	else
		return false;
	}


- Nick Nisi

stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 18
Solved Threads: 4
 

wow... try this....

Random random = new Random(); 
boolean a = random.nextBoolean();
tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

wow... try this....

Random random = new Random(); 
boolean a = random.nextBoolean();

Worked great. I was looking in Math and not Random.

Thanks

vex
Newbie Poster
22 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You