Monthly Archives: September 2006

Visual Basic Power Pack, CTPs and SPs, Oh my!

Lots of code has been flowing out of Redmond recently:

  • Two Visual Basic 2005 Power Packs have been released. The Microsoft Interop Forms Toolkit 1.0 and Microsoft PrintForm Component 1.0 are both targeted at people who use COM and VB6 and should make migration of code easier in many cases. You can get them here, and more are likely to come.
  • Beta 1 of VS 2005 Service Pack 1 has been released. This service pack includes a significant amount of work on the part of the Visual Basic compiler team and addresses a lot of the major issues (performance or otherwise) that we’ve found after release. I encourage people to try it out and report any problems you run into with it (I’ve got it installed!). You can get it on Microsoft Connect.
  • The September Community Technology Preview (CTP) of Orcas has been released. Unlike the previous LINQ CTPs, this CTP is actual Orcas production code instead of a prototype implementation. As a result, many of the features present in the last LINQ CTP aren’t in this CTP, so it’s going to be a step back in that regard. Also, because of the way the CTP schedule and the VB compiler schedule happened to (not) sync up, there are not a lot of new VB features in this CTP. Expect much more in the coming CTPs! You can get the CTP here (and hurrah! We’re finally moving to Virtual PC images!).

Enjoy all the tasty code!

Pseudo-Random Numbers, VB and Doing the (Mersenne) Twist…

Interesting how random things sometimes come together. I was checking out Chris William’s pre-beta rogue-like game Heroic Adventure! on CodePlex and noticed that although most of the game is written in VB, he had included some C# code in there. The motivation was the fact that the pseudo-random number generators that VB and the .NET platform provide (i.e. VB’s Rnd function and .NET’s System.Random) are not strong enough to satisfy the random number needs of his game. So he borrowed some C# code that implements the Mersenne Twister algorithm, which is a much better pseudo-random number generator.

I thought it was a shame for Chris to have to sully all his beautiful VB code with some C# (obligatory <g> for the humor impaired), so I took a look at the reference implementation of the algorithm and coded it up in VB (making sure, of course, the test output matched that of the reference implementation). For those of you with random number needs, I’ve attached it at the end. Please let me know if you find bugs.

Interestingly, at almost exactly the same time, the question of the strength (or lack thereof) of our pseudo-random number generator came up internally. Keep in mind that VB .NET’s Rnd function was written to remain completely compatible with VB 6.0’s Rnd function, which was compatible with VB 5.0’s Rnd function, etc., etc., all the way back to VB 1.0’s Rnd function. So that pseudo-random number generator is pretty freaking old — 15+ years and counting! We’re discussing what we might do about the situation, but since some people may depend on setting a particular seed and then getting a predictable sequence…

One caveat to keep in mind is that none of these pseudo-random number generators are strong enough to be used for cryptographic uses. For that, try System.Security.Cryptography.RNGCryptoServiceProvider.

'
' An implementation of the Mersenne Twister algorithm (MT19937), developed
' with reference to the C code written by Takuji Nishimura and Makoto Matsumoto
' (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).
'
' This code is free to use for any pupose.
'

Option Strict On

''' 
''' A random number generator with a uniform distribution using the Mersenne 
''' Twister algorithm.
''' 
Public Class MersenneTwister
    Private Const N As Integer = 624
    Private Const M As Integer = 397
    Private Const MATRIX_A As UInteger = &H9908B0DFUI
    Private Const UPPER_MASK As UInteger = &H80000000UI
    Private Const LOWER_MASK As UInteger = &H7FFFFFFFUI

    Private mt(N - 1) As UInteger
    Private mti As Integer = N + 1

    ''' 
    ''' Create a new Mersenne Twister random number generator.
    ''' 
    Public Sub New()
        Me.New(CUInt(Date.Now.Millisecond))
    End Sub

    ''' 
    ''' Create a new Mersenne Twister random number generator with a
    ''' particular seed.
    ''' 
    ''' The seed for the generator.
    Public Sub New(ByVal seed As UInteger)
        mt(0) = seed
        For mti = 1 To N - 1
            mt(mti) = CUInt((1812433253UL * (mt(mti - 1) Xor (mt(mti - 1) >> 30)) + CUInt(mti)) And &HFFFFFFFFUL)
        Next
    End Sub

    ''' 
    ''' Create a new Mersenne Twister random number generator with a
    ''' particular initial key.
    ''' 
    ''' The initial key.
    Public Sub New(ByVal initialKey() As UInteger)
        Me.New(19650218UI)

        Dim i, j, k As Integer
        i = 1 : j = 0
        k = CInt(IIf(N > initialKey.Length, N, initialKey.Length))

        For k = k To 1 Step -1
            mt(i) = CUInt(((mt(i) Xor ((mt(i - 1) Xor (mt(i - 1) >> 30)) * 1664525UL)) + initialKey(j) + CUInt(j)) And &HFFFFFFFFUI)
            i += 1 : j += 1
            If i >= N Then mt(0) = mt(N - 1) : i = 1
            If j >= initialKey.Length Then j = 0
        Next

        For k = N - 1 To 1 Step -1
            mt(i) = CUInt(((mt(i) Xor ((mt(i - 1) Xor (mt(i - 1) >> 30)) * 1566083941UL)) - CUInt(i)) And &HFFFFFFFFUI)
            i += 1
            If i >= N Then mt(0) = mt(N - 1) : i = 1
        Next

        mt(0) = &H80000000UI
    End Sub

    ''' 
    ''' Generates a random number between 0 and System.UInt32.MaxValue.
    ''' 
    Public Function GenerateUInt32() As UInteger
        Dim y As UInteger
        Static mag01() As UInteger = {&H0UI, MATRIX_A}

        If mti >= N Then
            Dim kk As Integer

            Debug.Assert(mti <> N + 1, "Failed initialization")

            For kk = 0 To N - M - 1
                y = (mt(kk) And UPPER_MASK) Or (mt(kk + 1) And LOWER_MASK)
                mt(kk) = mt(kk + M) Xor (y >> 1) Xor mag01(CInt(y And &H1))
            Next

            For kk = kk To N - 2
                y = (mt(kk) And UPPER_MASK) Or (mt(kk + 1) And LOWER_MASK)
                mt(kk) = mt(kk + (M - N)) Xor (y >> 1) Xor mag01(CInt(y And &H1))
            Next

            y = (mt(N - 1) And UPPER_MASK) Or (mt(0) And LOWER_MASK)
            mt(N - 1) = mt(M - 1) Xor (y >> 1) Xor mag01(CInt(y And &H1))

            mti = 0
        End If

        y = mt(mti)
        mti += 1

        ' Tempering
        y = y Xor (y >> 11)
        y = y Xor ((y << 7) And &H9D2C5680UI)
        y = y Xor ((y << 15) And &HEFC60000UI)
        y = y Xor (y >> 18)

        Return y
    End Function

    ''' 
    ''' Generates a random integer between 0 and System.Int32.MaxValue.
    ''' 
    Public Function GenerateInt32() As Integer
        Return CInt(GenerateUInt32() >> 1)
    End Function

    ''' 
    ''' Generates a random integer between 0 and maxValue.
    ''' 
    ''' The maximum value. Must be greater than zero.
    Public Function GenerateInt32(ByVal maxValue As Integer) As Integer
        Return GenerateInt32(0, maxValue)
    End Function

    ''' 
    ''' Generates a random integer between minValue and maxValue.
    ''' 
    ''' The lower bound.
    ''' The upper bound.
    Public Function GenerateInt32(ByVal minValue As Integer, ByVal maxValue As Integer) As Integer
        Return CInt(Math.Floor((maxValue - minValue + 1) * GenerateDouble() + minValue))
    End Function

    ''' 
    ''' Generates a random floating point number between 0 and 1.
    ''' 
    Public Function GenerateDouble() As Double
        Return GenerateUInt32() * (1.0 / 4294967295.0)
    End Function
End Class

Updated 09/22/2006: The dingo ate my XML comments! Fixed.

Updated 09/22/2006 (Later): The dingo ate my <g> as well! Fixed.

Lang .NET 2006 talk posted

If you are curious about my talk at the Lang .NET symposium that I talked about a while ago, you can now download the video of the talk here. As is usual, I can’t bear to watch the damn thing since I think my voice sounds just awful–it’s so much nicer sounding in my head. Oh well.

Overall, I think the talk was only OK. I kind of switched around what I was going to talk about late in the game and so I don’t think it was as interesting as I was hoping to be. It talks some about some of the issues we’ve run into with implementing LINQ and some thoughts about some future direction, but we had a snafu with the time (I thought I had much less time than I really did), so it kind of got truncated at the end. I’m hoping to make up for some of it on my blog this fall…

Localized programming languages

Omer van Kloten‘s entry on Internationalization of Programming reminded me of a (possibly apocryphal) story that I was told when I started working on OLE Automation. I asked why IDispatch::GetIDsOfNames takes an LCID and was told that once-upon-a-time, the VBA team conducted an experiment in localization with VBA in Excel (which was the first application to host VBA). Apparently, they attempted to localize the entire language–keywords, function names, etc.–into French, and possibly other languages. This mean you could write code along the lines of what Omer outlines in his entry, except in French instead of Dutch.

The problem was that because VBA wasn’t exactly compiled in those days, the Excel spreadsheet that you wrote your code in now depended on having localized Excel on the machine. If you sent your spreadsheet to your colleague in New York, they couldn’t run the macros because their English Excel didn’t understand the language…

Catch me at the Capital Area .NET Users group!

As I intimated a few posts ago, I’m going to be back on the East Coast at the end of the month. Part of the reason is to visit family (my family this time), but part of it is to give a talk at the Capital Area .NET Users group. You can find their website at http://www.caparea.net/, and here’s the blurb:

Visual Basic 9.0: Language Integrated Query (LINQ), XML integration and beyond…

Tuesday, September 26, 2006 at 7:00 PM

With its second version on the .NET Framework, Visual Basic has largely completed the process of moving from its previous home in COM to its new home in the CLR. As a full-fledged language on a premier runtime platform, the inevitable next question is: Now what? Come hear a discussion and see demos of where Visual Basic is headed in the future. Visual Basic 9.0 will offer radical improvements in its ability to query data in all its forms, whether as objects, XML, or relational data. Also, working with data interchange becomes significantly easier as XML becomes integrated directly into the language. And, finally, we’ll take a look back at Visual Basic’s dynamic language and scripting roots to see what lessons from the past might be brought into future versions and look ahead at where the language might be headed beyond 9.0.

The talk will mostly cover LINQ and will be a more developed version of the talk I gave at the PDC (i.e. it will include all the work and thinking that we’ve done since last November). It’ll also have some teaser stuff at the end that covers a bit of what we talked about at Lang .NET (which I’ll also discuss here once I’ve dug out a bit more).

If you’re in the VA/DC/MA area, I hope to see you there!

Two wild and crazy guys…

Normally, I don’t promote videos and stuff that I haven’t actually watched, but since I’m still buried under vacation emails, and I know that it’s just got to be good stuff, I recommend checking out:

Erik Meijer: Democratizing the Cloud

and

Brian Beckman: Monads, Monoids, and Mort

Erik and Brian are just two crazy guys with lots of crazy ideas who’ve been a lot of fun to interact with over the past couple of years… Without Erik, there’d be no XML support coming in VB and he’s added a lot to our LINQ discussions. And, well, I haven’t quite figured Brian out yet, but I’m working on it! <g>

How I Spent My Summer Vacation

Well, I’m at the tail end of my East Coast summer vacation, which has included a trip to the beach (Oak Island, NC) and a trip to see the in-laws (Richmond, VA).

Highlights:

  • Getting my 9 year old niece and 11 year old nephew hooked (nay, obsessed) with the board games Settlers of Catan and Ticket to Ride. I mean, I love those games, but man… I think they would play Settlers 24/7 if they could. Frightening.
  • Taking one last ride on the Hurricane at the Myrtle Beach Pavilion. This was the fourth summer we’d taken the kids to the Pavilion, and we’re sad that it’ll be the last.
  • Having lunch on our way back to VA at Parker’s Barbeque and Chicken in Wilson, NC. It’s always a good sign when the parking lot is full on a weekday for lunch. I don’t think the barbeque or Brunswick stew was as good as the ones at Bullocks in Durham, but since I’m going to be visiting there in another month, I wasn’t too broken up. It was, however, nice to be in a real down-home NC eatery. Felt like home.

Lowlights:

  • Catching the tail-end of tropical-whatever Ernesto. Hopefully, my nephew’s laser tag birthday won’t be flooded out down in Shockoe Bottom.
  • We ended up at Parker’s because the place we really wanted to try, the Beefmaster Inn, wasn’t open for lunch. I guess we’ll have to make the trek out one day when we’re visiting Durham…

Back at work soon…