Re: random ints on symmetric interval
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: random ints on symmetric interval         

Group: comp.lang.fortran · Group Profile
Author: Ron Ford
Date: Sep 17, 2008 00:25

On Tue, 16 Sep 2008 23:27:15 -0800, glen herrmannsfeldt posted:
> Ron Ford wrote:
>
>
>> I *do* have off by one bug, but I think we're both wrong at this point, as
>> your classical way to roll a die has uneven outcomes.
>
> All will have slightly uneven outcome unless i_tot evenly
> divides the appropriate generator value, often the number
> of possible return values and often a power of two. But...
>
>> call random_number(harvest)
>> b = min(max(1,ceiling(harvest*sides)),sides)
>
> is different that the previously suggested:
>
>>> b = -i_max + int( harvest * i_tot )
>
> ceiling(harvest*sides) mostly returns a value between
> 1 and sides, with an extremely low probability of zero.
>
> max(1,ceiling(harvest*sides)) the min doesn't do anything.
>
> removes even that small probability. sides=t_tot=2*i_max+1,
> the result is between 1 and 2*_max+1.
> Later you subtract i_max, so between 1-imax and imax+1.
>
> It seems you don't have bounds checking on, and you don't
> notice the counts in k(i_max+1)
>
> Either use floor() or int(), or subtract one.

Thanks for your response, Glen. We seemed to be posting at the same time
here. I made a second response to elliot that ruminates about the above.
I won't believe anything until Steve Kargl chimes in.
>
> As far as the original question, I believe it is
> one fourth, not one eighth.
>
> If you have two (approximately) symmetric distributions
> uniform over, for example, the range (-1/2,1/2), that is,
> the PDF is rect(x)
>
> http://en.wikipedia.org/wiki/Rectangular_function
>
> (Note that the result doesn't depend on rect(1/2) or rect(-1/2).)
>
> The PDF of the difference is tri(x)
>
> (the convolution of rect with itself)
>
> http://en.wikipedia.org/wiki/Triangular_function
>
> which has one fourth of its area outside (-1/2,1/2).
>
> -- glen

How did we miss half of the cases?

%%- pete said:
%%-
%%- > Richard wrote:
%%- >
%%- >
%%- >> Whats wrong with merely
%%- >>>
%%- >>> int mycmp(const void *a, const void *b)
%%- >>> {
%%- >>> return (*(int *)p2)-(*(int *)p1);
%%- >>> }
%%- >>
%%- >> other than the obvious typo of the variable names course ...
%%- >
%%- > There are about (2 * INT_MAX) situations,
%%- > where the result is undefined.
%%- >
%%- > Example:
%%- > if (*(int *)p2) equals INT_MAX
%%- > and (*(int *)p1) equals (-1),
%%- > then your program becomes meaningless.
%%-
%%- I agree with your reasoning but not with your mathematics.
%%-
%%- Let's take, for example, a simple four-bit int (with INT_MIN being
-INT_MAX
%%- - 1). Sure, it's non-conforming, but it illustrates the point and keeps
%%- the arithmetic simple.
%%-
%%- INT_MAX is 7. UB is invoked when the values are:
%%-
%%- 7 and any of -1, -2, -3, -4, -5, -6, -7, -8
%%- 6 and any of -2, -3, -4, -5, -6, -7, -8
%%- 5 and any of -3, -4, -5, -6, -7, -8
%%- 4 and any of -4, -5, -6, -7, -8
%%- 3 and any of -5, -6, -7, -8
%%- 2 and any of -6, -7, -8
%%- 1 and any of -7, -8
%%- 0 and -8
%%-
%%- so it does seem that the number of UB cases is S(n + 1) where n is the
%%- value of INT_MAX and S(n) = n(n+1)/2.
%%-
%%- For an INT_MAX of 32767, for example, the number of UB cases is
536854528,
%%- which is 16384 * INT_MAX, not 2 * INT_MAX.
%%-
%%- --
%%- Richard Heathfield <http://www.cpax.org.uk>
%%- Email: -http://www. +rjh@
%%- Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
%%- "Usenet is a strange place" - dmr 29 July 1999

Is it that we didn't think of the negatives in first position:
-7 - +3
?
--
It is inaccurate to say that I hate everything. I am strongly in favor of
common sense, common honesty, and common decency. This makes me forever
ineligible for public office.
H. L. Mencken
no comments
diggit! del.icio.us! reddit!