Quantcast c++ probability question? - XP Math - Forums
XP Math Home Sign Up FREE! | Sign In | Classroom Setup | Common Core Alignment PDF Version

Go Back   XP Math - Forums > Welcome > Off-Topic Discussion

Reply
 
Thread Tools Display Modes
Old 08-01-2007   #1
waterlooguy
Guest
 
Posts: n/a
Default c++ probability question?

i want to write c++ code, that selects the following number with certain probability. Number 1 with probability 65/100Number 2 with probability 35/100How do i write the code?
  Reply With Quote
Old 08-01-2007   #2
pingerlee
Guest
 
Posts: n/a
Default

N1 = 65/100n2 = 35/100
  Reply With Quote
Old 08-01-2007   #3
thefreevariable
Guest
 
Posts: n/a
Default

I haven't written c++ in a long time, but it would go something like this (comments included).double x; // declare your variableint r = rand(99); // get a random integer 0-99if (r>34) x = 1; // 65/100 chanceelse x = 2; // 35/100 chanceReplace 1 and 2 with whatever the numbers are.Actually you might be able to shorten that using the ternary operator.double x = (rand(99) > 34) ? 1 : 2;Yeah that's a way more elegant solution.
  Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
XP Math