aspose file tools*
The moose likes Beginning Java and the fly likes Picking variables with a given propability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Picking variables with a given propability" Watch "Picking variables with a given propability" New topic
Author

Picking variables with a given propability

Olaf Olafson
Greenhorn

Joined: Nov 19, 2010
Posts: 2
Hi guys,

I have assigned a value of propability to a variable p of 20% (0,2)

then I have two other variables int a = 10 and int b = 5

How can I code it that way, that with 20% propability a is picked and with 80% propability
b is picked?


btw this is my first post, so hi to all of you. I am a beginner in java fooling around with some code ;)

br Olaf



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Welcome to JavaRanch!

That is very easy: for example, generate a random floating-point number between 0 and 1 and if it is < 0.2, pick the first one, otherwise pick the other one.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Olaf Olafson
Greenhorn

Joined: Nov 19, 2010
Posts: 2
hmm great, thanks!, does make sence! I´ll try to implement it ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Try it with the ? : operator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Picking variables with a given propability