Google Groups Home
Help | Sign in
Message from discussion generating random integers
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jim Mack  
View profile
 More options May 16, 10:50 pm
Newsgroups: microsoft.public.vb.general.discussion
From: "Jim Mack" <jm...@mdxi.nospam.com>
Date: Fri, 16 May 2008 22:50:06 -0400
Local: Fri, May 16 2008 10:50 pm
Subject: Re: generating random integers

Bob O`Bob wrote:
> ...or generating random Longs, whatever.

> I am unsure if I have a flawed code fragment or if I am running into
> the oft-mentioned shortcomings of VB's PRNG (psuedo-random number
> generator)

> I have a function designed to return a random integer from within
> the supplied range (inclusive) and the code looks like this:

>      Private Function MyRnd(lowerbound As Long, upperbound As Long)
>          As Long MyRnd= CLng((upperbound - lowerbound + 1) * Rnd +
>      lowerbound) End Function

The canonical method would be Int(Rnd * (UB - LB + 1)) + LB

CLng is not recommended because of its 'even' bias, and the LB should
be added after the Int.

I'm pretty sure that's been in the MS Basic docs since forever.

--
        Jim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google