Google Groups Home
Help | Sign in
generating random integers
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
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
Bob O`Bob  
View profile
 More options May 16, 10:08 pm
Newsgroups: microsoft.public.vb.general.discussion
From: Bob O`Bob <filter...@yahoogroups.com>
Date: Fri, 16 May 2008 19:08:15 -0700
Local: Fri, May 16 2008 10:08 pm
Subject: generating random integers
...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

I thought that I had derived this from the example in VB's own help files,
but now I see that the example there is far too simple.

Anyway, as a quality check, I've been looping a few tens of thousands
of times on it, accumulating the results, and I'm not liking what I see.

Did I get the formula wrong?
I can't seem to figure out where it really came from.

        Bob
--


    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.
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.
Richard Mueller [MVP]  
View profile
 More options May 16, 11:48 pm
Newsgroups: microsoft.public.vb.general.discussion
From: "Richard Mueller [MVP]" <rlmueller-nos...@ameritech.nospam.net>
Date: Fri, 16 May 2008 22:48:57 -0500
Local: Fri, May 16 2008 11:48 pm
Subject: Re: generating random integers

The Rnd function returns values greater than or equal to 0 and less than 1.
You should use the Randomize function once at the start to seed it. It has a
period of 2^24, so values repeat after 16,777,216 calls to the function.

The function internally produces a psuedo random integer between 0 and
16,777,215, then divides by 16,777,216.

I think the best way to generate longs between lowerbound and upperbound
would be:

MyRnd = Fix((upperbound - lowerbound + 1) * Rnd) + lowerbound

The CLng function rounds, so the max and min values are less likely than
other values. The Fix function takes the integer part. Values from 0.000 to
0.499 round to 0, while 0.5 to 1.499 round to 1 (twice as likely). But 0.000
to .999 Fix to 0, while 1.0 to 1.999 Fix to 1.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


    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.
Bob O`Bob  
View profile
 More options May 17, 1:08 am
Newsgroups: microsoft.public.vb.general.discussion
From: Bob O`Bob <filter...@yahoogroups.com>
Date: Fri, 16 May 2008 22:08:47 -0700
Local: Sat, May 17 2008 1:08 am
Subject: Re: generating random integers

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

Thanks!  /That/ was what I was running into.

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

I thought something like it too, but the VB6 help versions seem
to show only the horribly simplified single example now.

        Bob
--


    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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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