473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

java giving 50% chance

i know math.random can randomly generate numbers, but i want to know is there a way to let the system decides which variable to choose from a and b?
i want to set both can randomly be selected (50 - 50) chance?
Apr 9 '08 #1
6 16476
r035198x
13,262 8TB
i know math.random can randomly generate numbers, but i want to know is there a way to let the system decides which variable to choose from a and b?
i want to set both can randomly be selected (50 - 50) chance?
Math.random generates numbers between 0.0 and 1.0
Half of that is 0.5.
Ideally then, numbers in [0.0, 0.5) have a 50% chance of getting generated.
Apr 9 '08 #2
Math.random generates numbers between 0.0 and 1.0
Half of that is 0.5.
Ideally then, numbers in [0.0, 0.5) have a 50% chance of getting generated.
yeah...but is there other way then math.random? just curious..
Apr 9 '08 #3
r035198x
13,262 8TB
yeah...but is there other way then math.random? just curious..
java.util.Random ?
Apr 9 '08 #4
BigDaddyLH
1,216 Expert 1GB
java.util.Random?
Indeed. Method Math.random just uses java.util.Random anyway, and the class has a bunch of useful methods. Method Math.random is just leftover from the early API. When you think you need a source of randomness you should check out class Random first.
Apr 9 '08 #5
drsmooth
112 100+
if u use the java.util.Random, you could use

Expand|Select|Wrap|Line Numbers
  1. Random r = new Random(); 
  2. int choice = r.nextInt(2);
  3. if(choice==0)
  4.  //choice 1
  5. else
  6.  //choice b
thats what i would recommend, the nextInt(2); would generate either a 1 or a 0, at what i would assume is a 50-50 chance.
Apr 11 '08 #6
BigDaddyLH
1,216 Expert 1GB
if u use the java.util.Random, you could use

Expand|Select|Wrap|Line Numbers
  1. Random r = new Random(); 
  2. int choice = r.nextInt(2);
  3. if(choice==0)
  4.  //choice 1
  5. else
  6.  //choice b
thats what i would recommend, the nextInt(2); would generate either a 1 or a 0, at what i would assume is a 50-50 chance.
And if it's a turn-left/turn-right you'd be wanting, there's a method for that in Random. No need to generate 0/1 and then turn it into true/false:
Expand|Select|Wrap|Line Numbers
  1. if (rnd.nextBoolean()) {
  2.     ...
  3. } else {
  4.     ...
  5. }
Apr 11 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: kk | last post by:
I read this mailing list fairly often, and I am always amazed at what I learn (even not related to Python). I am relatively new to Python. I make my living developing mostly in Java. Python was a...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
4
by: hopefull | last post by:
any chance of a version of VB that compiles to java byte code? then Sun would have a proper development langauge <g>
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.