By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 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
Share this Question
Share on Google+
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" <no*****@nowhere.com> 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
Hi darrel,

First of all, the data is in a DataTable in the DataSet, not in a DataSet.
It's important to keep this in mind. A DataSet cannot be sorted. In fact,
neither can a DataTable. But a DataView (a view of a DataTable) can. How you
do this is dependent upon how your DataSet is bound to whatever it may be
bound to, which you didn't say.

But I'll skip over that part; hopefully you can figure out the details. I
will give you the principles and basics of what you need to do.

First, you need an extra column in your DataTable. This can be created
either during the fetching of the data from the database, or afterwards, To
do it during the fetching of the data, you can create either a View, a
Stored Procedure or SQL Statement that creates the extra column. The
principle is this:

Consider a simple Table named Company having 3 columns: Name, Address,
Phone. You can fetch the entire contents of the table with a simple SQL
Statement:

SELECT Name, Address, Phone FROM Company

You can add a fourth column that is a number quite easily, with a slight
modification:

SELECT Name, Address, Phone, 0 AS SortOrder FROM Company

If using SQL Server, or a similar database that has random number
functionality, you can do (something like):

SELECT Name, Address, Phone, RAND() As SortOrder FROM Company

Now you have your (fouth) sorting column.

If you didn't use a Random number function in the query, you can randomize
the column now. You create your DataSet, containing a DataTable, and then
loop through the rows of the DataTable, replacing the 0 in the SortOrder
column with a random number for each. Note that this doesn't limit you to 26
values, but as many as you wish.

When you display the DataTable, you are actually using a View, either a
custom View or the DefaultView property of the DataTable. So, all you have
to do then is set the Sort property of the View to the column SortOrder.

Easy, peasy Japaneasy!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.

"darrel" <no*****@nowhere.com> 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 #3

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.