Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is the radom number generation not right?

Below is my code:

uniform_int_distribution<>temp(-1,1);
default_random_engine e((unsigned int)time(0));
for(int a=0;a<50;a++){
cout<<temp(e)<<endl;
}

You know, I expect the number range is -1,0,1 ,these three numbers. Actually the result normally only contains 0 and 1. If I run this program several times, then, seldom the -1 number will be last number of one time. I don't know why, uniform_int_distribution should contains -1, right?

like image 522
jeffchen Avatar asked Dec 17 '25 22:12

jeffchen


1 Answers

This is a bug that has apparently been fixed: http://connect.microsoft.com/VisualStudio/feedback/details/712984

If you need a workaround right now without installing software updates, why not just generate between 0 and 2, then subtract 1 afterwards?

like image 56
paddy Avatar answered Dec 19 '25 16:12

paddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!