473,498 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random

Hi i need to generate a random string , can anybody give me an example of
how to do it in c#

thanks!
Nov 16 '05 #1
5 4559
Hi Ariel,

This sounds a little bit like some homework assignment :P but here goes.

This method will make a random string of a given length out of small and
capital letters using ascii A-Z, a-z,

private string GenerateString(int length)
{
Random rand = new Random();
string randomString = "";
for(int i = 0; i < length; i++)
{
int small = rand.Next(2);
int letter = rand.Next(26);
if(small == 0)
letter += 65;
else
letter += 97;
randomString += Convert.ToChar(letter);
}
return randomString;
}
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
H

Check the following ur
http://www.obviex.com/Samples/Password.asp

HT
Ravikanth[MVP]
Nov 16 '05 #3
Hi,

You could use the random generator, and then use a StringBuilder to append
them , all you have to do is make sure you generate printables characters.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenezarrobahotmail.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi i need to generate a random string , can anybody give me an example of
how to do it in c#

thanks!

Nov 16 '05 #4
Thanks Ignacio, Ravikanth, and Mortenfor your help, you Rocks!!!!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eK**************@tk2msftngp13.phx.gbl...
Hi,

You could use the random generator, and then use a StringBuilder to append them , all you have to do is make sure you generate printables characters.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenezarrobahotmail.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi i need to generate a random string , can anybody give me an example of how to do it in c#

thanks!


Nov 16 '05 #5
"Ariel Gimenez" <arielgimenezarrobahotmail.com> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
Hi i need to generate a random string , can anybody give me an example of
how to do it in c#


If your objective is to get something irregular and/or unique you could use

System.Guid.NewGuid.ToString()
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3651
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
6
20685
by: Acacia | last post by:
How would you generate a random number in C++?
10
2481
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
5948
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
2484
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
15
2941
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
4
2735
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
8
3893
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
6
4791
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7381
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
5465
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,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
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
1424
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 ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.