November 2003 - Posts
Ron just asked me to demo how I thought the Random guy selector would be implemented so, I thought I'd crank out a quickie demo. First, I'll create a Guy type:
Class Guy
Private mName As String
Private mScore As Integer
Public Sub New(ByVal name As String, ByVal score As Integer)
Me.mName = name
Me.mScore = score
End Sub
Public ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
Public ReadOnly Property Score() As Integer
Get
Return mScore
End Get
End Property
Public Overrides Function ToString() As String
Return Me.mName & " ( " & Me.mScore & " ) "
End Function
End Class
This example populates a collection of guys on init of a process and then handles an event to randomly select a guy from the collection. Using the logic mentioned in the previous post, a guy with a higher "Score" should have a higher probability of pushing the randomly generated target below zero:
Private mGuys As New ArrayList
Private mSumOfGuys As Integer
Private Sub InitailizeGuys(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim names As String() = {"Jim", "Bob", "Wayne", "Foo", "Darren", "Barry"}
Dim r As New Random
For Each s As String In names
Dim guy As New Guy(s, r.Next(100))
mGuys.Add(guy)
mSumOfGuys += guy.Score
LabelGuys.Text &= guy.ToString & vbCrLf
Next
End Sub
Private Sub PickARandomGuy(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetRandomGuyButton.Click
Dim r As New Random
Dim randScore As Integer = r.Next(mSumOfGuys)
For Each g As Guy In mGuys
randScore -= g.Score
If randScore <= 0 Then
LabelRandomGuy.Text = "New guy is : " & g.ToString
Exit Sub
End If
Next
End Sub
Here's Ron's results (formatted). I ran a test picking a "Guy" 10000000 times and here is the result:
Player Weight Weighted Probability Occurance (10000000 iterations)
====== ====== ===================== ===============================
Jim 20 05.87 06.34
Bob 10 02.93 03.02
Wayne 40 11.73 11.91
Foo 5 01.47 01.48
Darren 90 26.39 26.35
Barry 22 06.45 06.57
Randy 45 13.20 13.36
John 11 03.23 03.11
Doe 98 28.74 27.86
I couldn't help but blog this. On the [C# lang] list the following question was asked:
I want to generate a random number within a specified range.
Say there are a bunch of players on my basketball team who usually sit on
the bench. Each player is ranked, e.g.
John 5
Bill 1
Derek 10
etc.
The higher the rank, the more chances this player will be chosen to to play.
To which Eddie Garmon replied:
instead of using an array that has each item added for the # of increaded(sic) possibilities, do a little simple math instead.
sum up the total possible outcomes(sum of all players weight) use this as your rand max, and get your random # randomize your player array.
walk the array subtracting the weight of each player from your random # when your random # is less than 0, use that player.
I liked that :-)
The other day I noticed that Cory Smith blogged somewhere that he was planning to write some articles about how to build "Windows Media Player"-Like UI's.... looks like he's just posted the first article just now.
[ cross-posted on
http://regexblogs.com/DNeimke ]
Senkwe Chand left the following comment on my last entry and I thought that I'd answer it here to clear any confusion that might exist:
Hi Darren, I guess I should really direct this to ScottW, but, in order to have a blog listed at regexblogs, doesn't that imply that you need to have a .Text (and therefore .Net) powered blog? If so, I doubt that you'll get many (if any) Java, Ruby or Perl afficionados maintaining blogs there. I really hope I'm wrong about that because I've just started work on my own regex tool and would love to have a blog hosted there at some point :-)
Having a blog at regexblogs.com doesn't require you to have any software of your own at all. Essentially, we (http://regexadvice.com) host the application on our servers and provide you with an account and free space to manage your own content. The content is owned by you, we simply manage/host the blog software; in this case .Text.
.Text is an excellent piece of blog'ging software and has tons of features. On top of that, because it is used for the blogs at weblogs.asp.net you can be pretty sure that it will be kept up to date with the current trends as the blogging community continues to evolve the standard for this kind of application.
So, in answer to your question Senkwe, if you sign up for a blog with regexblogs.com you won't have to maintain any software, hardware etc., you simply need internet access to login and add your entries! :-)
Also, just to be clear, the site's focus is regular expressions, not .NET regular expressions.
I was messing around with collections today and decided to try and work out how the HashTable stores/returns it's items so, I ran the following test:
Dim nums As Integer() = {99, 4, 60, 110, 5}
Dim ht As New Hashtable
For Each num As Integer In nums
ht.Add(num, num)
Next
Label1.Text = ""
For Each key As Object In ht.Keys
Label1.Text &= key.ToString() & " :: " & key.GetHashCode() & Environment.NewLine
Next
I'm not sure exactly what that means with regard to how the enumerator algorithms traverses it's children but, I'm kinda guessing that, under the covers the HashTable uses an algorithm to re-order its children to stop it becoming lopsided which probably resulted in a structure resembling:
110
/ \
5 60
/ \
4 99
While on the subject of structures, I just noticed that Scott Mitchell has a new series starting on them at Msdn : Read Part 1 here.
[ cross-posted on http://regexblogs.com/DNeimke ]
If this entry has landed in your email/aggregator/browser and you'd like a home to express your regex thoughts regularly, please contact me via my contact form and I can set you up. Likewise, if you know of a keen regex'er of any flavour, please mention this offer to them.
One of my biggest hopes is that, over time many Java, Ruby and Perl regex afficionado's will maintain a blog there.
Aside from the obvious daily|weekly rant-style postings, I'd love to see the follow types of blogs start to appear:
- Blogging while reading Jeff Friedl's Mastering Regex book; I know that I wish I had. I can still remember - vividly in fact - sitting on the bus, reading the book and crying out "NO, he's wrong! It doesn't work that way"! Alas, many of those thoughts are either lost between pages 1 and 420 or, simply left behind on the bus.
- Anybody that has written about or taught regular expressions.
- Anybody invloved with regex either on the .NET Framework or Mono projects
- Anybody that has had to construct regex's for mission-critical app's - such as the ASP.NET parser that tokenizes webforms.
I trust that, in time the regex blogging community will flourish into an energetic place from which new regex ideas, thoughts and concepts are derived!
I'm hoping to make time to catch Chris Kinsman's talk in Adelaide on Friday:
Presentation Overview
Chris Kinsman, Regional Director at Microsoft is flying out from Seattle to deliver a special presentation on ASP.NET. Following Chris's presentation, Sean Salisbury, Technical Expert from Compuware will present the tools and technologies that will help you deliver better code, faster.
Date:
Friday November 21
Time:
12:00 midday - 2:00pm
Venue:
Microsoft Office Adelaide Theatre
Level 26, Santos House
91 King Street
Adelaide SA 5000
I realize that most hard-core backenders will already know most of this but, with all of the posts that have entered my aggregator relating to sprocs and ad-hoc sql, I thought that I'd take this opportunity to add a link to an article I wrote a few years ago which discusses what gets cached (and when):
http://www.msdnaa.net/Resources/Display.aspx?ResID=825
(Listening To: Out of the City (Hardkandy Mix) [2 Heads / Chillout Sessions Vol 4 (Disc 2)])
I read the new "Beyond (COM) Add Reference: Has Anyone Seen the Bridge?" article yesterday and, was quite impressed by the detailed groundwork that was laid-out to explain the differences between COM and CLR and, how Interop provides a bridge for seamlessly calling in and out of both environments. As the author states in that article the real meat is still to come so, if you are reading this and you haven't already read that article scoot over now and give it a look - you won't be disappointed.
I'm really looking forward to having Interop explained to me, not so much because the "Add COM reference" behaviour in VS.NET doesn't work - it does! - but more because I know that someday I'll be involved in a project where Interop will go wrong and I want to have some idea of what I'm dealing with. What's more, having the classes in the System.Runtime.InteropServices namespace explained will add yet another tick to the namespaces that I've taken the time to learn about ;-)
After reading that article, I stumbled across this blog entry that would have meant very little to me had I not read the Interop article (which I suppose serves to validate the fact that my knowledge store has increased).
Bonus Msdn rating system rant...
One thing that confounds me is the ratings system on Msdn. When you rate an article (as I did with this one) you are asked to leave a message indicating your thoughts about the article. I wish that Microsoft would make these comments visible as it would provide a better insight into the overall rating. For example, the guy who rated Sam's article a 1 out of 9 - it would be insightful (and add perspective) to see *why* he thought that it smelled *that* bad. Also, if you rate an item more than 5 points above or below the current average, it should be moderated (based on the comment that is left) as to whether it actually gets added, this would add integrity to the overall rank.
(Listening To: Clubbed to Death (Kurayamino Version) [Rob Dugan / Chillout Sessions Vol 4])
Duncan, gave my mark-up tool a nice plug today but missed a very important feature. It is emminently better to mark-up code snippets using Css styles (as opposed to Html tags) because you can have global control over how your code samples are rendered. To set the tool to output Css styles do the following:
Options / Output Format / Css
Your code snippets will now be rendered to use Css classes rather than embedded font tags. You can then use the tool to generate a stylesheet definition for you:
Options / Generate Stylesheet
...and use that in your blog by adding it as a custom selector:
Admin / Options / Configure
Scroll down to the section titled "Custom CSS Selectors" and add the following Css definition:
PRE.CodeSnippet
{
background-color : #EEEEEE;
padding : 4px;
margin : 0px;
border : solid 1px #d0c8b7;
font-family : "Lucinda Console";
font-size : 1em ;
color : #000000;
}
.VbNet_InlineComment {
font-family: "Lucinda Console"; color: Green;
}
.VbNet_String {
font-family: "Lucinda Console"; color: Red;
}
.VbNet_DataType {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Function {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Keyword {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Method {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Object {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Operator {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Property {
font-family: "Lucinda Console"; color: Blue;
}
.VbNet_Statement {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_BlockComment {
font-family: "Lucinda Console"; color: Green;
}
.CSharp_XmlComment {
font-family: "Lucinda Console"; color: Silver;
}
.CSharp_InlineComment {
font-family: "Lucinda Console"; color: Green;
}
.CSharp_String {
font-family: "Lucinda Console"; color: Red;
}
.CSharp_ReferenceType {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_ValueType {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_Modifier {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_Statement {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_MethodParameter {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_Namespace {
font-family: "Lucinda Console"; color: Blue;
}
.CSharp_Keyword {
font-family: "Lucinda Console"; color: Blue;
}
.TSql_BlockComment {
font-family: "Lucinda Console"; color: Teal;
}
.TSql_InlineComment {
font-family: "Lucinda Console"; color: Teal;
}
.TSql_String {
font-family: "Lucinda Console"; color: Red;
}
.TSql_SysTable {
font-family: "Lucinda Console"; color: LawnGreen;
}
.TSql_SysSproc {
font-family: "Lucinda Console"; color: Brown;
}
.TSql_GlobalVariable {
font-family: "Lucinda Console"; color: Fuchsia;
}
.TSql_ReservedKeyword {
font-family: "Lucinda Console"; color: Blue;
}
.TSql_Function {
font-family: "Lucinda Console"; color: Fuchsia;
}
.TSql_DataType {
font-family: "Lucinda Console"; color: Blue;
}
.TSql_Operator {
font-family: "Lucinda Console"; color: Silver;
}
.TSql_SystemFunction {
font-family: "Lucinda Console"; color: Brown;
}
More Posts
Next page »