Discussion:
Random Char String
(too old to reply)
MrT
2008-05-24 02:38:20 UTC
Permalink
Hi:

C++ Builder 5 dual core Pentium 2.8 ghz 2 gig memory

I have routine that generates random char sequence for password.

I have saved information in txt file below for testing.

I first generate a random 55 char string then pull out 8 char starting
at random location.

If I let it run at full speed it generates the exact same sequence:

Id Id1 random 55 random 8


685 6 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 7 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 14 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 115 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 109 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 99 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 21 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 29 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 11 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 30 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 32 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 121 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 118 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 35 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 37 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 46 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 51 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 106 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 66 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 69 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 70 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 86 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 95 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 101 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 103 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 108 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 104 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 105 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 116 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 110 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 111 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 113 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 114 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 122 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081
685 123 720114u51623911208191257302558f8n5109h188421ek7z8967061 39112081

If I run the exact same code but break at each iteration, it works fine:

685 6 e46x72262i342642317317147r4865659667428248577842817v772 26423173
685 7 i74r88692w5735p26v832x211422346983409324543y46139730320 92w5735p
685 14 46h88925587n6515u5033830074177821812244555u622259802983 h8892558
685 115 11087092845948607mnmt55qu894t63b63709066598599185g5374x 07mnmt55
685 109 755754620304270y43f1832532295534653678b410017197176l955 55754620
685 99 969p2aj7709817o1a47857591e7w7994335409396g63161305g4zz7 j7709817
685 21 43774217028283l9cgm5q089966188926725452282988796202950m 37742170
685 29 7wq8218o2489461191t07541169v2081s010173227603ve474iwh64 8218o248
685 11 1x08904g4337481539684034449671z461719r13740ikx8vx818409 04g43374
685 30 2319719119b4tk48839856415g7y2n82645815722803u220m94x66f 4tk48839
685 32 u23027652y41311450j51432d5343918855601359g5615n55026667 27652y41
685 121 m18y3b412137r42615285b5502i8976807552d24382746f8q204c40 b412137r
685 118 03791415834c0799346740a01jn60240800784t39n29f9583q42001 79934674
685 35 y219233g097g47g93c2087345860f76c242d15128665u66633o11k7 93c20873
685 37 3884692981721210083w4374r479244q2250m8f3929x0v61420h242 84692981
685 46 7p35i3k068369179715326741z5a30r445315674854583682518389 69179715
685 51 1039948710975148s7d9j83058345u118122i794e141z1080469828 5148s7d9
685 106 x8vb55737k48864d22388747ho0215o24y82633882729s867287839 737k4886
685 66 3404596214342u25a416i31t0072785120174al9769685042g43973 96214342
685 69 7v2c18904091v52x14098o6740n606023649ir1s9340504734u3526 2x14098o
685 70 k83377269h8938q430m051296u3l5672569511973f5612811533181 8q430m05
685 86 b2150q24c04i83369b70073c5de58e0l9q27m614qc9185972i40096 24c04i83
685 95 tg53a1001710r743180137ja470p555m759223a313270841s741467 r7431801
685 101 737t0913594785775451z5k296t509411235302g594239778890986 47857754
685 103 91su94490663980e81e54e5410658v0352626019t265146b982129z 44906639
685 108 4897713f280tz3c4722402989ia4110108lq7745986j336t54e037z 3c472240
685 104 5095jy49310wz361550g8n493x27jo2827887889036810082712470 5095jy49
685 105 43l56663354g5m909v6907147918m1848vf31101161496021357278 909v6907
685 116 85z887t182y6852924997637j87897279r239fd23g8040t533662q4 85292499
685 110 693968j1640741z773c493o9856249c50k186658895k77111527262 693968j1
685 111 259069464x183255e6078109862674124i48687r288079029839873 255e6078
685 113 l03u1161z56x44121256921a604708d407r4y8274994cnt23295h6o x4412125
685 114 d30503412u3x58t9277763r3706702206i460024118r8704384f253 30503412
685 122 v180689321733g0p01054l8r2g8114509e4t28ho811y50288411d82 33g0p010
685 123 n118414688947172ib66be2w9931479533696428971752938s2330f 172ib66b


Is there some problem with timing in using random????

Thanks

Best regards
unknown
2008-05-24 02:26:07 UTC
Permalink
Post by MrT
I have routine that generates random char sequence for password.
Is there some problem with timing in using random????
No, there shouldn't be.
You didn't show your code, so we can only guess
that you are calling randomize() every time.
It should only be called once at startup.
MrT
2008-05-24 16:50:49 UTC
Permalink
Post by unknown
Post by MrT
I have routine that generates random char sequence for password.
Is there some problem with timing in using random????
No, there shouldn't be.
You didn't show your code, so we can only guess
that you are calling randomize() every time.
It should only be called once at startup.
Only calling once at startup:

__fastcall TMainDM::TMainDM(TComponent* Owner)
: TDataModule(Owner)
{
CRInvoiceDate = 0;
MR = 0;
TempId = 0;
SavedPaymentYear = 0;
Linux = false;
Randomize();
}



void __fastcall TAssocDM::RepopulateAssocPasswords(int id)
{
String RawPassword;
String Password;
TStringList *SList;
SList = new TStringList;
SList->Clear();
int AssocId;
int a;
char SQLStatement[100] = {0};
if (!IBAssocTransaction->Active)
IBAssocTransaction->StartTransaction();
IBAssocPWUpdate->SelectSQL->Clear();
IBAssocPWUpdate->SelectSQL->Add("select ID, ASSOCID,STATUS_MEM, PWORD
from ASSOC_C");
IBAssocPWUpdate->SelectSQL->Add("where STATUS_MEM = 'ACTIVE' AND");
sprintf(SQLStatement, " ID = %i", id);
IBAssocPWUpdate->SelectSQL->Add(SQLStatement);
IBAssocPWUpdate->Prepare();
IBAssocPWUpdate->Open();
IBAssocPWUpdate->First();
while (!(IBAssocPWUpdate->Eof))
{
AssocId = IBAssocPWUpdate->FieldByName("ASSOCID")->AsInteger;
Password = MainDM->GetNewPassword(AssocId);
RawPassword = Password;
a = random(16);
Password = Password.SubString(a + 1,8);
SList->Add(IntToStr(id) + ' ' + IntToStr(AssocId) + ' ' +
RawPassword + ' ' + Password);
IBAssocPWUpdate->Edit();
IBAssocPWUpdate->FieldByName("PWORD")->AsString = Password;
IBAssocPWUpdate->Post();
IBAssocPWUpdate->Next();
Password = "";
}
SList->SaveToFile("PWordList.txt");
SList->Free();
IBAssocTransaction->CommitRetaining();
IBAssocPWUpdate->Close();
}

//---------------------------------------------------------------------------
//Generate Random Password
//---------------------------------------------------------------------------
String __fastcall TMainDM::GetNewPassword(Seed)
{
char ch;
int a;
int x = 55; //26; Recently changed to 55 for testing. originally 26
String PassWord = "";
// RandSeed = Seed;

time_t t;
srand((unsigned) time(&t)); <-------------------
for (int i = 0; i < x; i++)
{
a = random(random(3));
if (a == 0) {ch = char(48 + random(10));} //Random 4 0-9..
if (a == 1) {ch = char(65 + random(26));} //--||-- 4 A-Z..
if (a == 2) {ch = char(97 + random(26));} //--||-- 4 a-z..
if ((ch != ' ') && int(ch) < 127 && int(ch) > 32 && ch != NULL){
PassWord = PassWord + ch;
}
else {
x = x - 1;
}
}
PassWord = LowerCase(PassWord);
return PassWord;
}



srand((unsigned) time(&t)); reinitializes but with different seed.
Should not cause problem.

What has this got to do with the fact that when delayed by break it
works fine?

Thanks

Best regards
MrT
2008-05-24 17:06:21 UTC
Permalink
I removed

srand((unsigned) time(&t)); <-------------------

That was the culprit. Why would that be?

Thanks
Post by MrT
Post by unknown
Post by MrT
I have routine that generates random char sequence for password.
Is there some problem with timing in using random????
No, there shouldn't be.
You didn't show your code, so we can only guess
that you are calling randomize() every time.
It should only be called once at startup.
__fastcall TMainDM::TMainDM(TComponent* Owner)
: TDataModule(Owner)
{
CRInvoiceDate = 0;
MR = 0;
TempId = 0;
SavedPaymentYear = 0;
Linux = false;
Randomize();
}
void __fastcall TAssocDM::RepopulateAssocPasswords(int id)
{
String RawPassword;
String Password;
TStringList *SList;
SList = new TStringList;
SList->Clear();
int AssocId;
int a;
char SQLStatement[100] = {0};
if (!IBAssocTransaction->Active)
IBAssocTransaction->StartTransaction();
IBAssocPWUpdate->SelectSQL->Clear();
IBAssocPWUpdate->SelectSQL->Add("select ID, ASSOCID,STATUS_MEM, PWORD
from ASSOC_C");
IBAssocPWUpdate->SelectSQL->Add("where STATUS_MEM = 'ACTIVE' AND");
sprintf(SQLStatement, " ID = %i", id);
IBAssocPWUpdate->SelectSQL->Add(SQLStatement);
IBAssocPWUpdate->Prepare();
IBAssocPWUpdate->Open();
IBAssocPWUpdate->First();
while (!(IBAssocPWUpdate->Eof))
{
AssocId = IBAssocPWUpdate->FieldByName("ASSOCID")->AsInteger;
Password = MainDM->GetNewPassword(AssocId);
RawPassword = Password;
a = random(16);
Password = Password.SubString(a + 1,8);
SList->Add(IntToStr(id) + ' ' + IntToStr(AssocId) + ' ' +
RawPassword + ' ' + Password);
IBAssocPWUpdate->Edit();
IBAssocPWUpdate->FieldByName("PWORD")->AsString = Password;
IBAssocPWUpdate->Post();
IBAssocPWUpdate->Next();
Password = "";
}
SList->SaveToFile("PWordList.txt");
SList->Free();
IBAssocTransaction->CommitRetaining();
IBAssocPWUpdate->Close();
}
//---------------------------------------------------------------------------
//Generate Random Password
//---------------------------------------------------------------------------
String __fastcall TMainDM::GetNewPassword(Seed)
{
char ch;
int a;
int x = 55; //26; Recently changed to 55 for testing. originally 26
String PassWord = "";
// RandSeed = Seed;
time_t t;
srand((unsigned) time(&t)); <-------------------
for (int i = 0; i < x; i++)
{
a = random(random(3));
if (a == 0) {ch = char(48 + random(10));} //Random 4 0-9..
if (a == 1) {ch = char(65 + random(26));} //--||-- 4 A-Z..
if (a == 2) {ch = char(97 + random(26));} //--||-- 4 a-z..
if ((ch != ' ') && int(ch) < 127 && int(ch) > 32 && ch != NULL){
PassWord = PassWord + ch;
}
else {
x = x - 1;
}
}
PassWord = LowerCase(PassWord);
return PassWord;
}
srand((unsigned) time(&t)); reinitializes but with different seed.
Should not cause problem.
What has this got to do with the fact that when delayed by break it
works fine?
Thanks
Best regards
Ed Mulroy [TeamB]
2008-05-24 16:31:42 UTC
Permalink
You only want to call randomize() once.

srand((unsigned) time(&t)); is essentially the source code for randomize()

. Ed
Post by MrT
I removed
srand((unsigned) time(&t)); <-------------------
That was the culprit. Why would that be?
MrT
2008-05-24 22:41:57 UTC
Permalink
Post by Ed Mulroy [TeamB]
You only want to call randomize() once.
srand((unsigned) time(&t)); is essentially the source code for randomize()
But if called with the time as a seed, why the repetition?
Post by Ed Mulroy [TeamB]
. Ed
Post by MrT
I removed
srand((unsigned) time(&t)); <-------------------
That was the culprit. Why would that be?
Alan Bellingham
2008-05-24 23:36:05 UTC
Permalink
Post by MrT
But if called with the time as a seed, why the repetition?
Perhaps because when you do it all at once, the time doesn't get to
change? After all, feed the same number in each time, you'll get the
same number out.

Next time, seed your random number generator *once* when the program
starts, and resist the temptation to do it again.

Alan Bellingham
--
Team Browns
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette
Alan Bellingham
2008-05-25 09:30:44 UTC
Permalink
Post by MrT
But if called with the time as a seed, why the repetition?
To clarify, just in case you're not aware, and for any who are
interested.

A random number generator is an algorithm that produces numbers that are
evenly spread, and apparently unpredictable. But of course, being a
algorithm, it is *totally* predictable - if you start with the same
number, and apply the same calculations, you end up with the same
result. And thus, there is a single long repeating sequence.

(The entirety of the number isn't necessarily visible at the output -
that may be getting 32 bits of what is an actual 64 bit calculation, for
instance.)

For testing purposes, it's extremely useful to be able to put the
internal state into a known state - hence the idea of 'seeding' it. You
effectively set the entire number to a known value and, because it's an
algorithm and therefore entirely predictable, it's entirely repeatable.
It's equivalent to stating where in the long sequence you want to start.

Using the current time as a seed means that between runs, you should end
up using different sequences (or rather, different parts of the same
enormous long sequence) for different runs. Always assuming that you're
getting different times.

The problem here was that the starting point was being reset every time.
And since the time was the same (it ticks over once a *second*), the
pure repeatability became very obvious: the sequence wasn't being
followed, because the effect was to jump back to the start each time.

The above describes the simple random number generator supplied by
rand() and the like. There are more sophisticated ones, and other ways
of getting semi-random inputs to break up predictability (network
latency measurements, time between keyboard strokes, etc.). The best
methods do have some truly indeterminable inputs, such as random thermal
noise in specially built circuitry.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
unknown
2008-05-25 00:39:02 UTC
Permalink
Post by MrT
Randomize();
Randomize() is a different function than randomize();
Post by MrT
a = random(16);
randomize() goes with random();
Post by MrT
srand((unsigned) time(&t)); <-------------------
On a fast CPU, time() doesn't change very often.
That is how you are resetting the generator, as I surmised.
MrT
2008-05-25 21:00:16 UTC
Permalink
Hi:

Thanks guys. That explains it all. I thought it grabbed milliseconds
since blah.

This was originally on a much slower computer and it seemed to work.

I tested with a delay (processmessages) and it produced blocks of
different results depending on delay.
Post by unknown
Post by MrT
Randomize();
Randomize() is a different function than randomize();
Post by MrT
a = random(16);
randomize() goes with random();
Post by MrT
srand((unsigned) time(&t)); <-------------------
On a fast CPU, time() doesn't change very often.
That is how you are resetting the generator, as I surmised.
Loading...