All entries for September 2013

September 19, 2013

VB and C# functions generate the same 'random' strings each time

I'm using a standard VB.net pattern to generate random usercodes;

Function GenerateUsercode()
Dim s As String = "abcdefghijklmnopqrstuvwxyz"
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To 8
Dim idx As Integer = r.Next(0, s.Length)
sb.Append(s.Substring(idx, 1))
Next
Return sb.ToString()
End Function

However, if I call it twice, quickly, it generates the same 'random' usercode. Here's what it looks like when I call it with:

dim pass1 as string = GenerateUsercode()
dim pass2 as string = GenerateUsercode()
System.Diagnostics.Debug.Write(pass1 & " " & pass2)

Output:

uerpajfv uerpajfv

It looks like something is being cached. If I set a breakpoint and step throgh the code, it generates two random strings. I tried resetting things; reDIMming all the variables after they'd been used, resetting the stringbuilder to zero length, and started Googling in earnest. This blog had the answer; The problem is that the 'Dim r As New Random' line in the function is being executed so quickly (presumably a combination of fast processor, very good compliler optimisation) that the random seed is being created with the same value each time it's generated.

There's actually no need to regenerate the seed every time because the

r.Next

gets the next random number based on the seed. The solution is to move the 'Dim r As New Random' out of the function to the main delarations area where it executes once, and the pattern works perfectly.


September 10, 2013

FIM Oracle MA

If you're struggling to configure the Oracle MA in FIM, there's an excellent guide to installing the Oracle client on a Windows server here.

I struggled even with this guide; I downloaded and merged both sets of driver files, created environment variables, and ran the ODBC_install app which flashed-up a dos box breifly, but gave no indication whether it had worked. When I ran the Windows Data Sources (ODBC) tool and tried to add a new User DSN, no Oracle driver was listed.

Turns out that the Oracle Drivers are 32-bit drivers; there's no 64-but version. And on a 64Bit Windows server, the Windows Data Sources (ODBC) tool lists only 64 bit drivers. You have to open a command prompt and specifically run the 32bit version of the Windows Data Sources tool:

c:\windows\SysWOW64\odbcad32.exe

The Oracle driver will be listed; select it and set-up your odbc connection as usual.


September 2013

Mo Tu We Th Fr Sa Su
Aug |  Today  | Oct
                  1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30                  

Search this blog

Tags

Galleries

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXXIII