Getting a random letter.

D

darrel

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
 
S

sloan

Find the char value of "a", and then add your random number....

From
http://www.codingforums.com/archive/index.php?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;
}
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top