Total # of cases when a - b = 0; 5 return 5
Total # of cases when a-b = 1; 4   return 4
Total # of cases when a-b = 2; 3   return 3

Sorry, it is misleading...it should be

Total # of cases when a - b = 0; 5 return 5 is probable 5 times
Total # of cases when a-b = 1; 4   return 4 is probable 4 times
Total # of cases when a-b = 2; 3   return 3 is probable 3 times and so on...

On Mon, Aug 29, 2011 at 11:17 PM, Don <[email protected]> wrote:

> No, a+b-1 would return values outside of the desired range.
> Don
>
> On Aug 29, 12:26 pm, nishaanth <[email protected]> wrote:
> > @Don...Nice solution.. But the return statement should be a+b-1
> >
> >
> >
> > On Mon, Aug 29, 2011 at 10:33 PM, Don <[email protected]> wrote:
> > > If you draw the nxn grid and assign a value to each diagonal:
> >
> > > (For n = 5)
> >
> > >  ----------b
> > > |  12345
> > > |  2345
> > > |  345
> > > |  45
> > > |  5
> > > a
> >
> > > You want the result to be the orthogonal distance from the diagonal.
> > > That is what the formula computes.
> > > Don
> >
> > > On Aug 29, 11:28 am, Piyush Grover <[email protected]> wrote:
> > > > I understand what you are doing in the loop but return statement is
> not
> > > > clear to me. Can you explain.
> >
> > > > On Mon, Aug 29, 2011 at 9:48 PM, Don <[email protected]> wrote:
> > > > > int custRand(int n)
> > > > > {
> > > > >        int a,b;
> > > > >        do
> > > > >        {
> > > > >                a = rand(n);
> > > > >                b = rand(n);
> > > > >        } while(a < b);
> > > > >        return n - a + b;
> > > > > }
> >
> > > > > On Aug 29, 10:48 am, Piyush Grover <[email protected]>
> wrote:
> > > > > > Given a function rand(n) which returns a random value between
> 1...n
> > > > > assuming
> > > > > > equal probability.
> > > > > > Write a function CustRand(n) using rand(n) which returns a value
> > > between
> > > > > > 1...n such that
> > > > > > the probability of occurrence of each number is proportional to
> its
> > > > > value.
> >
> > > > > > I have a solution but wondering if I can get better than this or
> some
> > > > > other
> > > > > > approaches:
> >
> > > > > > CustRand(n){
> >
> > > > > >     sum  = n*(n+1)/2;
> >
> > > > > >     a = rand(sum);
> > > > > >     for(i = 1; i <= n; i++){
> > > > > >          h = i*(i+1)/2;
> > > > > >          l = i*(i-1)/2;
> > > > > >          if(a <= h && a > l)
> > > > > >              return i;
> > > > > >     }
> >
> > > > > > }
> >
> > > > > --
> > > > > 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?hl=en.
> >
> > > --
> > > 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?hl=en.
> >
> > --
> > S.Nishaanth,
> > Computer Science and engineering,
> > IIT Madras.
>
> --
> 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?hl=en.
>
>

-- 
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?hl=en.

Reply via email to