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

Thread Tools Display Modes
Old 08-01-2007   #1
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?
Old 08-01-2007   #2
Posts: n/a

N1 = 65/100n2 = 35/100
Old 08-01-2007   #3
Posts: n/a

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.

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 03:45 PM.

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