Question:
Add a new member function to the random number class of the previous project. The new member function generates the next pseudorandom member but does not return the number directly. Instead, the function returns this number divided by the modulus. (You will have to cast the modulus to a double number before carrying out the division; otherwise, the division will be an integer division, throwing away the remainder.)
The return value from this new member function is a pseudorandom double number in the range [0, 1). (The square bracket, [, indicates that the range does include 0, but the rounded parenthesis, ), indicates that the range goes up to 1, without actually including 1.)
My answer:
修改random.h
#ifndef RANDOM_H
#define RANDOM_H
class pseudorandom_generator{
public:
pseudorandom_generator(int a, int b, int c, int d){
multiplier = a;
increment = b;
modulus = c;
seed = d;
};
void set_parameters(int a, int b, int c, int d);
int generate_number();
double generate_double_number();
private:
int multiplier, seed, increment, modulus;
};
#endif
在random.cpp中添加
double pseudorandom_generator::generate_double_number(){
seed = (multiplier*seed + increment)%modulus;
double t1 = (double)seed;
double t2 = (double)modulus;
cout << t1/t2 << " ";
return t1/t2;
}
修改main.cpp
#include "random.h"
#include <iostream>
using namespace std;
int main(){
pseudorandom_generator a(40, 725, 729, 1);
cout << "******start to generate pseudorandom numbers******" << endl;
int i = 0;
while(i < 729){
// a.generate_number();
a.generate_double_number();
if((i+1)%20 == 0)
cout << "\n";
i++;
}
cout << "\n******here should be numbers in [0, 1) uniquely******" << endl;
return 0;
}
结果:
******start to generate pseudorandom numbers******
0.0493827 0.969822 0.78738 0.489712 0.58299 0.314129 0.559671 0.381344 0.248285 0.925926 0.0315501 0.256516 0.255144 0.200274 0.00548697 0.213992 0.554184 0.161866 0.469136 0.759945
0.392318 0.687243 0.484225 0.363512 0.534979 0.39369 0.742112 0.679012 0.155007 0.194787 0.786008 0.434842 0.388203 0.522634 0.899863 0.989026 0.555556 0.216735 0.663923 0.55144
0.0521262 0.079561 0.176955 0.0727023 0.902606 0.0987654 0.94513 0.799726 0.983539 0.336077 0.437586 0.497942 0.912209 0.482853 0.308642 0.340192 0.602195 0.0823045 0.286694 0.462277
0.485597 0.418381 0.729767 0.185185 0.40192 0.0713306 0.847737 0.903978 0.153635 0.139918 0.591221 0.643347 0.728395 0.130316 0.207133 0.279835 0.187929 0.51166 0.460905 0.430727
0.223594 0.938272 0.525377 0.00960219 0.378601 0.138546 0.536351 0.44856 0.9369 0.470508 0.814815 0.587106 0.478738 0.144033 0.75583 0.227709 0.102881 0.109739 0.384088 0.358025
0.315501 0.61454 0.576132 0.0397805 0.585734 0.423868 0.949246 0.964335 0.567901 0.710562 0.41701 0.674897 0.990398 0.610425 0.411523 0.455418 0.211248 0.444444 0.772291 0.886145
0.440329 0.607682 0.301783 0.0658436 0.628258 0.124829 0.987654 0.500686 0.0219479 0.872428 0.891632 0.659808 0.386831 0.467764 0.705075 0.197531 0.895748 0.824417 0.971193 0.84225
0.684499 0.374486 0.973937 0.951989 0.0740741 0.957476 0.293553 0.736626 0.459534 0.375857 0.0288066 0.146776 0.865569 0.617284 0.685871 0.429355 0.168724 0.743484 0.733882 0.349794
0.986283 0.445816 0.82716 0.0809328 0.231824 0.26749 0.694102 0.758573 0.337449 0.492455 0.69273 0.703704 0.142661 0.70096 0.0329218 0.311385 0.449931 0.99177 0.665295 0.60631
0.246914 0.871056 0.836763 0.465021 0.595336 0.807956 0.312757 0.504801 0.186557 0.45679 0.266118 0.639232 0.563786 0.545953 0.832647 0.300412 0.0109739 0.433471 0.333333 0.327846
0.108368 0.329218 0.163237 0.524005 0.954733 0.183813 0.347051 0.876543 0.0562414 0.24417 0.761317 0.447188 0.88203 0.27572 0.0233196 0.927298 0.0864198 0.451303 0.0466392 0.860082
0.397805 0.906722 0.263374 0.529492 0.174211 0.962963 0.513032 0.515775 0.625514 0.0150892 0.59808 0.917695 0.702332 0.0877915 0.506173 0.241427 0.651578 0.0576132 0.29904 0.956104
0.238683 0.541838 0.668038 0.716049 0.636488 0.454047 0.156379 0.249657 0.980796 0.226337 0.048011 0.914952 0.592593 0.698217 0.923182 0.921811 0.866941 0.672154 0.880658 0.22085
0.828532 0.135802 0.426612 0.0589849 0.353909 0.150892 0.0301783 0.201646 0.0603567 0.408779 0.345679 0.821674 0.861454 0.452675 0.101509 0.0548697 0.1893 0.566529 0.655693 0.222222
0.883402 0.33059 0.218107 0.718793 0.746228 0.843621 0.739369 0.569273 0.765432 0.611797 0.466392 0.650206 0.00274348 0.104252 0.164609 0.578875 0.14952 0.975309 0.00685871 0.268861
0.748971 0.953361 0.128944 0.152263 0.085048 0.396433 0.851852 0.0685871 0.737997 0.514403 0.570645 0.820302 0.806584 0.257888 0.310014 0.395062 0.796982 0.8738 0.946502 0.854595
0.178326 0.127572 0.0973937 0.890261 0.604938 0.192044 0.676269 0.0452675 0.805213 0.203018 0.115226 0.603567 0.137174 0.481481 0.253772 0.145405 0.8107 0.422497 0.894376 0.769547
0.776406 0.0507545 0.0246914 0.982167 0.281207 0.242798 0.706447 0.252401 0.090535 0.615912 0.631001 0.234568 0.377229 0.0836763 0.341564 0.657064 0.277092 0.0781893 0.122085 0.877915
0.111111 0.438957 0.552812 0.106996 0.274348 0.96845 0.73251 0.294925 0.791495 0.654321 0.167353 0.688615 0.539095 0.558299 0.326475 0.0534979 0.134431 0.371742 0.864198 0.562414
0.491084 0.63786 0.508916 0.351166 0.0411523 0.640604 0.618656 0.740741 0.624143 0.960219 0.403292 0.1262 0.042524 0.695473 0.813443 0.532236 0.283951 0.352538 0.0960219 0.835391
0.410151 0.400549 0.0164609 0.652949 0.112483 0.493827 0.747599 0.898491 0.934156 0.360768 0.42524 0.00411523 0.159122 0.359396 0.37037 0.809328 0.367627 0.699588 0.978052 0.116598
0.658436 0.331962 0.272977 0.91358 0.537723 0.503429 0.131687 0.262003 0.474623 0.979424 0.171468 0.853224 0.123457 0.932785 0.305898 0.230453 0.21262 0.499314 0.967078 0.677641
0.100137 0 0.994513 0.775034 0.995885 0.829904 0.190672 0.621399 0.85048 0.0137174 0.54321 0.722908 0.910837 0.427984 0.113855 0.548697 0.942387 0.689986 0.593964 0.753086
0.11797 0.713306 0.526749 0.0644719 0.573388 0.930041 0.196159 0.840878 0.62963 0.179698 0.182442 0.292181 0.681756 0.264746 0.584362 0.368999 0.754458 0.17284 0.908093 0.318244
0.72428 0.965706 0.622771 0.90535 0.208505 0.334705 0.382716 0.303155 0.120713 0.823045 0.916324 0.647462 0.893004 0.714678 0.581619 0.259259 0.364883 0.589849 0.588477 0.533608
0.33882 0.547325 0.887517 0.495199 0.802469 0.0932785 0.725652 0.0205761 0.817558 0.696845 0.868313 0.727023 0.0754458 0.0123457 0.48834 0.528121 0.119342 0.768176 0.721536 0.855967
0.233196 0.322359 0.888889 0.550069 0.997257 0.884774 0.38546 0.412894 0.510288 0.406036 0.23594 0.432099 0.278464 0.133059 0.316872 0.66941 0.770919 0.831276 0.245542 0.816187
0.641975 0.673525 0.935528 0.415638 0.620027 0.79561 0.81893 0.751715 0.0631001 0.518519 0.735254 0.404664 0.18107 0.237311 0.486968 0.473251 0.924554 0.97668 0.0617284 0.463649
0.540466 0.613169 0.521262 0.844993 0.794239 0.76406 0.556927 0.271605 0.858711 0.342936 0.711934 0.471879 0.869684 0.781893 0.270233 0.803841 0.148148 0.920439 0.812071 0.477366
0.0891632 0.561043 0.436214 0.443073 0.717421 0.691358 0.648834 0.947874 0.909465 0.373114 0.919067 0.757202 0.282579 0.297668 0.901235 0.0438957 0.750343 0.00823045 0.323731 0.943759
0.744856 0.788752 0.544582 0.777778 0.105624 0.219479 0.773663 0.941015 0.635117 0.399177 0.961591 0.458162 0.320988 0.834019 0.355281 0.205761 0.224966 0.993141 0.720165 0.801097
0.0384088 0.530864 0.229081 0.15775 0.304527 0.175583 0.0178326 0.707819 0.30727 0.285322 0.407407 0.290809 0.626886 0.0699588 0.792867 0.709191 0.36214 0.48011 0.198903 0.950617
0.0192044 0.762689 0.502058 0.0768176 0.0672154 0.683128 0.319616 0.77915 0.160494 0.414266 0.565158 0.600823 0.0274348 0.0919067 0.670782 0.825789 0.0260631 0.037037 0.475995 0.0342936
0.366255 0.644719 0.783265 0.325103 0.998628 0.939643 0.580247 0.20439 0.170096 0.798354 0.928669 0.141289 0.646091 0.838134 0.51989 0.790123 0.599451 0.972565 0.897119 0.879287
0.165981 0.633745 0.344307 0.766804 0.666667 0.66118 0.441701 0.662551 0.496571 0.857339 0.288066 0.517147 0.680384 0.209877 0.389575 0.577503 0.0946502 0.780521 0.215364 0.609053
0.356653 0.260631 0.419753 0.784636 0.379973 0.193416 0.731139 0.240055 0.596708 0.862826 0.507545 0.296296 0.846365 0.849108 0.958848 0.348422 0.931413 0.251029 0.0356653 0.421125
0.839506 0.57476 0.984911 0.390947 0.632373 0.289438 0.572016 0.875171 0.00137174
******here should be numbers in [0, 1) uniquely******
Reference:
整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p124