473,395 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Random Number and String Generators

Hi,
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Thanks

Aug 22 '07 #1
7 2491
On Aug 22, 9:47 am, "BillG" <nos...@nospam.comwrote:
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.
Well, System.Random is the normal random number generator to use
in .NET. It will give you numbers - from there it's easy to create
random strings.

You should only create a single instance of System.Random and reuse it
rather than creating a new one each time you need it, by the way -
otherwise you may get repeated patterns because the instance is
initialized with the current time as the seed.

Jon

Aug 22 '07 #2
Hello,

Take a look.

/// Verify the random string
public string RandomString
{
get
{
string str = "";
while (true)
{
str = InternalRandomString;

if (! m_lastRandomString.Contains(str))
{
m_lastRandomString.Add( str);
break;
}
}
return str;
}
}

/// <summary>
/// Generates a m_random string with the given length
/// </summary>
/// <returns>Random string</returns>
private string InternalRandomString
{
get
{
StringBuilder builder = new StringBuilder();

char ch;

int size = m_random.Next(2 , 8);

for (int i = 0 ; i < size ; i++)
{
ch = Convert.ToChar(m_random.Next(97 , 122));

builder.Append(ch);
}

return builder.ToString();
}
}
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"BillG" wrote:
Hi,
Does anyone know of a site or have code for a function that will generate a
random string or random number?
I need one where I can tell it what type of value I need and where I can set
the length of the desired output.

Thanks

Aug 22 '07 #3
The repeated pattern was what I was getting. How can I go about stopping
this from happening?

I've got the random generators in a separate C# file from rest of code, with
intention of using it in other apps if needed.

Tried to add the 'Random r = new Random();' to top of file just inside the
public class section but couldn't get it to be seen in the procedures within
it.

thanks

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
On Aug 22, 9:47 am, "BillG" <nos...@nospam.comwrote:
>Does anyone know of a site or have code for a function that will generate
a
random string or random number?
I need one where I can tell it what type of value I need and where I can
set
the length of the desired output.

Well, System.Random is the normal random number generator to use
in .NET. It will give you numbers - from there it's easy to create
random strings.

You should only create a single instance of System.Random and reuse it
rather than creating a new one each time you need it, by the way -
otherwise you may get repeated patterns because the instance is
initialized with the current time as the seed.

Jon
Aug 22 '07 #4
On Aug 22, 10:38 am, "BillG" <nos...@nospam.comwrote:
The repeated pattern was what I was getting. How can I go about stopping
this from happening?

I've got the random generators in a separate C# file from rest of code, with
intention of using it in other apps if needed.

Tried to add the 'Random r = new Random();' to top of file just inside the
public class section but couldn't get it to be seen in the procedures within
it.
We'd have to see the exact code to know what's wrong.

You might want to use my StaticRandom class - see
http://pobox.com/~skeet/csharp/miscu...ticrandom.html
and
http://pobox.com/~skeet/csharp/miscutil

Jon

Aug 22 '07 #5
In response to your original question
>Does anyone know of a site or have code for a function that will generate a
random string or random number?
http://www.random.org/
>I need one where I can tell it what type of value I need and where I can set
the length of the desired output.
and here is the interface:
http://www.random.org/clients/http/

Regards,
Joachim

Aug 22 '07 #6
Here is the code I am using to generate random strings

public static string RandomString(int size, string legalChars)
{
Random r = new Random(1);
//string legalChars =
"123456789abcdefghijklmnopqrstuvwxzy";//ABCDEFGHIJKLMNOPQRSTUVWXZY";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
sb.Append(legalChars.Substring(r.Next(0, legalChars.Length -
1), 1));
return sb.ToString();
}

Thanks


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
On Aug 22, 10:38 am, "BillG" <nos...@nospam.comwrote:
>The repeated pattern was what I was getting. How can I go about stopping
this from happening?

I've got the random generators in a separate C# file from rest of code,
with
intention of using it in other apps if needed.

Tried to add the 'Random r = new Random();' to top of file just inside
the
public class section but couldn't get it to be seen in the procedures
within
it.

We'd have to see the exact code to know what's wrong.

You might want to use my StaticRandom class - see
http://pobox.com/~skeet/csharp/miscu...ticrandom.html
and
http://pobox.com/~skeet/csharp/miscutil

Jon
Aug 22 '07 #7
On Aug 22, 3:15 pm, "BillG" <nos...@nospam.comwrote:
Here is the code I am using to generate random strings

public static string RandomString(int size, string legalChars)
{
Random r = new Random(1);
//string legalChars =
"123456789abcdefghijklmnopqrstuvwxzy";//ABCDEFGHIJKLMNOPQRSTUVWXZY";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++)
sb.Append(legalChars.Substring(r.Next(0, legalChars.Length -
1), 1));
return sb.ToString();
}
You're specifying the same seed every time, so it will always generate
the same string.

Additionally, you're never going to see a "Z" in the string. You
should just use:

sb.Append(legalChars[r.Next(legalChars.Length)]);

Jon

Aug 22 '07 #8

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

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
7
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
15
by: felixnielsen | last post by:
This is something i have done before and i know its pretty simple, however i cant remember how it works exactly, and i need it i kinda hurry, so if someone would be so nice to drop a random number...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
0
by: urkel | last post by:
Hello everybody, I have these errors on C program that may be because of random number definition. I use an SSH to compile C like in Linux. May be you have idea whether the errors are due to the...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.