473,320 Members | 2,124 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,320 software developers and data experts.

Random.Next question

I'm trying to write a Password generating program which will fill one
of the fields in our Accounts Table with an eight-digit password made
up of numerals, upper and lower case letters. I'm generating a random
number from 48 to 122, then converting its ASCII value to a char and
concatenating it to the end of a string called sPassword. When
sPassword has eight characters, the string is updated to the accounts
record in our database.

THE PROBLEM I'm having is that in run time all eight characters of the
password end up being the same, ie YYYYYYYY. If I step through the
same code in debug mode, I get eight unique numbers, ie Ysdf365D. Can
anyone fill me in on what I'm doing wrong here?

Thanks in advance,

Andre Ranieri
private void PassGen(SqlConnection cn, string sAccountKey)
{
try
{
String sPassword = "";
SqlCommand cmd = cn.CreateCommand();
while (sPassword.Length<8)
{
int p = 0;
Random rGen = new Random();
p = rGen.Next(47,123);
if ((p>47 && p<91) || (p>96 && p<123)) {
sPassword += (char)p;
Console.WriteLine(p + " " + (char)p + " " + sAccountKey);
}
}
//Write password back to UDF2
String sSQL = "UPDATE tblMainAccounts SET UDF2 = '" + sPassword +
"' "
+ "WHERE (AccountKey = " + sAccountKey + ");";
cmd.CommandText = sSQL;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.Write(ex.Source + ex.Message);
}
Nov 15 '05 #1
2 4665
You want to create ONE Random object, and call Next repeatedly against that
object. Take
Random rGen = new Random();
out of the while loop. Better, create it outside the PassGen function, and
pass it in as a parameter.

Read about the Random constructor you are using. If you created the Random
objects fast enough, they are all initialized with the same time dependent
seed.
"Andre" <an**********@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
I'm trying to write a Password generating program which will fill one
of the fields in our Accounts Table with an eight-digit password made
up of numerals, upper and lower case letters. I'm generating a random
number from 48 to 122, then converting its ASCII value to a char and
concatenating it to the end of a string called sPassword. When
sPassword has eight characters, the string is updated to the accounts
record in our database.

THE PROBLEM I'm having is that in run time all eight characters of the
password end up being the same, ie YYYYYYYY. If I step through the
same code in debug mode, I get eight unique numbers, ie Ysdf365D. Can
anyone fill me in on what I'm doing wrong here?

Thanks in advance,

Andre Ranieri
private void PassGen(SqlConnection cn, string sAccountKey)
{
try
{
String sPassword = "";
SqlCommand cmd = cn.CreateCommand();
while (sPassword.Length<8)
{
int p = 0;
Random rGen = new Random();
p = rGen.Next(47,123);
if ((p>47 && p<91) || (p>96 && p<123)) {
sPassword += (char)p;
Console.WriteLine(p + " " + (char)p + " " + sAccountKey);
}
}
//Write password back to UDF2
String sSQL = "UPDATE tblMainAccounts SET UDF2 = '" + sPassword +
"' "
+ "WHERE (AccountKey = " + sAccountKey + ");";
cmd.CommandText = sSQL;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.Write(ex.Source + ex.Message);
}

Nov 15 '05 #2
Fred,

Thanks a bunch! It turns out I was seeding the Random so fast it was
using the time-dependent stamp for several at once. I moved the
Random rGen; line of code into Main and passed it as a paramater as
you suggested, it worked great.

Cheers,

Andre
Nov 15 '05 #3

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

Similar topics

4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
10
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...
6
by: Starbuck | last post by:
Hi In VB6 we used the following to create a unique random number - Function longSerial() As Long longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100), "0000000"))...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
8
by: Chu | last post by:
I'm developing a game - in the game, the character has a 1 in 1000 chance of finding a special weapon. The pseudo code idea is this: get a random number from 1 to 1000 If random number is "1"...
4
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
by: ssims | last post by:
I've got this code for a random GDI+ chart generator, and the demo works, but when I try to run it on my local machine I get a compiler error: Compiler Error Message: CS0117: 'Random' does not...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.