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

 XP Math - Forums c++ probability question?
 User Name Remember Me? Password
 Register Arcade Members List Mark Forums Read

 Thread Tools Display Modes
 08-01-2007 #1 waterlooguy Guest   Posts: n/a 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?
 08-01-2007 #2 pingerlee Guest   Posts: n/a N1 = 65/100n2 = 35/100
 08-01-2007 #3 thefreevariable Guest   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 Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Welcome     XP Math News     Off-Topic Discussion Mathematics     XP Math Games Worksheets     Homework Help     Problems Library     Math Challenges

All times are GMT -4. The time now is 09:58 AM.

 Contact Us - XP Math - Forums - Archive - Privacy Statement - Top

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