@harit random number means its output cant be predicted,it doesnt mean that the probability will be equal for any number of test runs.
e.g consider random number between 3-5 for 3 test runs if the first output is 4 second is 3 then to ensure same probability third output must be 5 which makes random number predictible Now, in my case rand() generates 0 and 1randomly So the result may depend upon what the output of rand() is at each stage. In your code to get random number between 0 and 1 replace rand() by rand()%2 On Tue, Oct 6, 2009 at 6:54 PM, harit agarwal <agarwalha...@gmail.com>wrote: > neeraj and anilkumarmyla > yr codes r not giving numbers with equal probability > here is the code > > > #include<iostream> > #include<time.h> > #include<stdlib.h> > using namespace std; > int random(int a,int b) > { > return (a+(rand()%(b-a+1))); > } > int random1(int a,int b) > { > int mid; > if(a==b) > return a ; > mid = a + (b-a)/2 ; > if( rand() ) > // replace by if( rand()%2 ) return random(mid,b) ; > else > return random(a,mid) ; > } > main() > { > int a,b; > cout<<"enter range\n"; > cin>>a>>b; > srand(time(0)); > for(int i=0;i<10;i++) > cout<<random(a,b)<<"\t"; > cout<<endl; > for(int i=0;i<10;i++) > cout<<random1(a,b)<<"\t"; > } > > input : > 2 5 > output: > 5 4 3 3 5 3 5 3 3 5 > 5 5 4 5 5 5 3 3 5 3 > > > > > > -- Neeraj Kumar B.Tech (Part III) Department of Computer Sc. & Engineering IT-BHU,Varanasi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---