473,573 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java giving 50% chance

47 New Member
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 16570
r035198x
13,262 MVP
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
wassssup
47 New Member
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 MVP
yeah...but is there other way then math.random? just curious..
java.util.Rando m ?
Apr 9 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
java.util.Rando m?
Indeed. Method Math.random just uses java.util.Rando m 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 New Member
if u use the java.util.Rando m, 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 Recognized Expert Top Contributor
if u use the java.util.Rando m, 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
1960
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 "discovery" I made a couple of years ago, and I love the language, but only get to use it at home for hobbies. With all the recent news: - ESR...
114
9737
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
2793
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" when I launched the IBM Distributed Debugger via D:\IBMDebug>idebug.exe -qdaemon -quiport=8000,8001 First, a bit of background. I am running DB2...
2
9209
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 problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
4
1171
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
4967
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 Parameter. I can call the first Method without Problems, the Parameter can be deserialized by the WebService. But if I want to call the second Method and...
33
9913
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 the following calculation: z = x! / (x- y)! The following code is my attempt and i was hoping for a point in the right direction as too where i...
0
3124
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 url,first it will ask for authentication.After giving username and password,the authentication will be confirmed and if the site has access right as...
318
10862
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 ... http://kingrazi.blogspot.com/2008/05/shootout-c-net-vs-java-benchmarks.html Just to keep the post on topic for my friends at comp.lang.c++, how do I play default windows...
0
7755
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7992
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8190
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8048
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5281
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3722
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1284
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.