448,601 Members | 1,592 Online
Need help? Post your question and get tips & solutions from a community of 448,601 IT Pros & Developers. It's quick & easy.

# Getting a random letter.

 P: n/a I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short of making a case statement with 26 options, is there a more streamlined way to get an integer between 1-26 translated into one of the letters? Also, in case there's a more efficient way to handle this, this is what I'm trying to accomplish: I have a dataset that will have about 1000 records. This is a list of companies ordered alphabetically. We need the list that is displayed to start with a random letter of the alphabet, so that "AAA Company Name" doesn't always get listed first (and get an unfair advantage). I'm grabbing the dataset, sorted alphabetically, then creating a random letter. I then loop through the dataset looking for the first company that has a first letter matching the random letter. I then start writing out the records from that point, and loop back to the beginning when I reach the end. Is there a better way to handle that? Maybe re-sort the DS without having to loop through it all? -Darrel Jun 28 '06 #1
4 Replies

 P: n/a Find the char value of "a", and then add your random number.... From http://www.codingforums.com/archive/...p?t-40281.html --tsql -- y = Convert.ToChar((Convert.ToInt32(x) + 128)); is how you would do it, below is a function that will do whole strings rather that just single characters. private string DoLameEncryption(string input) { string returnValue = string.Empty; char[] charArray = input.ToCharArray(); foreach(char c in charArray) returnValue += Convert.ToChar((Convert.ToInt32(c) + 128)); return returnValue; } "darrel" wrote in message news:Oc**************@TK2MSFTNGP04.phx.gbl... I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short of making a case statement with 26 options, is there a more streamlined way to get an integer between 1-26 translated into one of the letters? Also, in case there's a more efficient way to handle this, this is what I'm trying to accomplish: I have a dataset that will have about 1000 records. This is a list of companies ordered alphabetically. We need the list that is displayed to start with a random letter of the alphabet, so that "AAA Company Name" doesn't always get listed first (and get an unfair advantage). I'm grabbing the dataset, sorted alphabetically, then creating a random letter. I then loop through the dataset looking for the first company that has a first letter matching the random letter. I then start writing out the records from that point, and loop back to the beginning when I reach the end. Is there a better way to handle that? Maybe re-sort the DS without having to loop through it all? -Darrel Jun 28 '06 #2

 P: n/a > From http://www.codingforums.com/archive/...p?t-40281.html --tsql -- y = Convert.ToChar((Convert.ToInt32(x) + 128)); is how you would do it, Thanks, sloan! -Darrel Jun 28 '06 #4

 P: n/a How about just, select * from companies order by newId() --Peter Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "darrel" wrote: I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short of making a case statement with 26 options, is there a more streamlined way to get an integer between 1-26 translated into one of the letters? Also, in case there's a more efficient way to handle this, this is what I'm trying to accomplish: I have a dataset that will have about 1000 records. This is a list of companies ordered alphabetically. We need the list that is displayed to start with a random letter of the alphabet, so that "AAA Company Name" doesn't always get listed first (and get an unfair advantage). I'm grabbing the dataset, sorted alphabetically, then creating a random letter. I then loop through the dataset looking for the first company that has a first letter matching the random letter. I then start writing out the records from that point, and loop back to the beginning when I reach the end. Is there a better way to handle that? Maybe re-sort the DS without having to loop through it all? -Darrel Jun 28 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.