473,472 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting a random letter.

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 2375
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Malcolm Clift | last post by:
Hi All, I have this so far. import random things = xrange(int(raw_input("choose no of things (1-8)? "))) state =
7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
5
by: Ariel Gimenez | last post by:
Hi i need to generate a random string , can anybody give me an example of how to do it in c# thanks!
3
by: tshad | last post by:
Is there a good random number/letter generator out there? I just want to be able to generate a password that has letters and or numbers about 10 characters long. Thanks, Tom
24
by: NoName | last post by:
Perl: @char=("A".."Z","a".."z",0..9); do{print join("",@char)}while(<>); !!generate passwords untill U press ctrl-z Python (from CookBook):
3
by: John | last post by:
Hi How can I generate a random password of 8 characters (digits and letters) in vb? Thanks Regards
9
by: JimmyJava694 | last post by:
Hey everyone, I need help trying to mutate a String. For example.. I ask the person to input a number. Then I ask them to input a String. (The string they input can be any length, but must...
7
by: devilinthebox | last post by:
I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks. // February 8, 2008 // The "BlackJack" class. import java.awt.*;...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.