Re: Random integer formula?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Top Spin (ToppSpin_at_hotmail.com)
Date: 12/03/04


Date: Thu, 02 Dec 2004 22:29:30 -0800

On Thu, 2 Dec 2004 19:41:35 -0500, "Jim Mack" <jmack@mdxi.nospam.com>
wrote:

>Top Spin wrote:
>> On Wed, 1 Dec 2004 06:28:13 -0500, "Jim Mack" <jmack@mdxi.nospam.com>
>> wrote:
>>
>>
>> I ran it from the Immediate window using
>>
>> MinMaxDist 3,5
>>
>> The MsgBox popped up saying that INext was "6".
>>
>> I have since rewritten the function to save the random number in a
>> separate variable and added a berak if it happens again, but it
>> hasn't.
>>
>> The statement
>>
>> lNext = Int((Rnd() * (iMax - iMin + 1)) + iMin)
>>
>> should never return a value outside (iMin,iMax), right?
>
>More than "shouldn't", I believe that it "cannot", although I would have
>cast this somewhat differently:
>
>Int(Rnd * (iMax - iMin + 1)) + iMin
>
>Note that here, iMin is not part of what is seen by Int(). I don't think
>it makes any difference, I just would never do it.

It does make one difference: it permits the domain to include negative
numbers without having to worry about whether to use Int() or Fix(). I
think it is a cleaner formula.

>I wish that for testing you had broken the parts out so that at the
>point you got the trap you could see exactly what Rnd() had produced,
>because that's the only interesting part.

Me, too. I usually don't write long statements for just that reason
when developing. I may later combine them, but I usually break them
out so I can see intermediate results.

I have runs long tests since and no traps. I'm guessing it was a
coding error on my part.

--
Running MS VB 6.0 Pro (SP5)
PC: HP Omnibook 6000
OS: Win 2K SP-4 (5.00.2195)
Email: Usenet-20031220 at spamex.com
(11/03/04)


Relevant Pages

  • Re: Random integer formula?
    ... Top Spin wrote: ... > I ran it from the Immediate window using ... iMin is not part of what is seen by Int(). ... point you got the trap you could see exactly what Rndhad produced, ...
    (microsoft.public.vb.general.discussion)
  • [PATCH 4/8] Notifier chain update: die_chain changes
    ... int signr; ... Trap & Exception support ...
    (Linux-Kernel)
  • Re: Annoying compiler warning
    ... C89 says: "The representations of integral types shall define ... a trap value. ... int type. ...
    (comp.lang.c)
  • Re: struct assignment and indeterminate values
    ... struct {int a, b;} x, y; ... representation, even though the value of a member of the structure ... or union may be a trap representation". ... That looks as though, after the assignment, no undefined behavior has occurred, ...
    (comp.lang.c)
  • Re: Trap red/green phone button..
    ... can't use this method to trap the phone keys on Smartphone, ... public enum KeyModifiers: int ...
    (microsoft.public.dotnet.framework.compactframework)