File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Picking variables with a given propability

 
Olaf Olafson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olaf Olafson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm great, thanks!, does make sence! I´ll try to implement it ;)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it with the ? : operator.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic