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() )
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---