Problems with random() function

FindFilesAll function returns the list of fils in TStringList. When i use random function in on form create it generates the same number all the time when program starts. It also returns the same file all the time. How can i fix this? Any ideas?

On form create:

  TStringList *lResults = new TStringList;
  FindFilesAll("D:\\Documents", lResults);

  int ii = random(lResults->Count);
  AnsiString FileName = lResults->Strings[ii];

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


>   Randomize;

This is not a proper C++ function call.  It should be:

Hope that helps,
shouldn't you be calling

// generate random seed

instead of Randomize; ??

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maybe I'm missing something important, but I thought C++ did random numbers a little differently. First of all, the problem you're describing sounds like a seed problem. Random number generators aren't random, they're algorithmic and start with a seed value (usually the current time). Each seed value should give a seemingly "random" number, but the same seed value will give the same random number each time.

Now, when I do random numbers in C++, I usually use the following:
srand(unsigned int seed); // this function seeds the random number generator
  // you only need to seed the generator once.
int rand(); // this function generates a random number between 0 and RAND_MAX

Anyway, you might already know all this, but if you don't, then it might help. Good luck!
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

random() will create the same series of random numbers on each execution
unless you seed the random number generator with a different seed each time.
I usually seed the random number generator with the current time:



long num = random();

BTW, Kashra's example uses rand(), which is discouraged.  random() supercedes rand()
using a much better random number generator.

     rand, srand, sranddev, rand_r - bad random number generator

     These interfaces are obsoleted by random(3).

     random, srandom, srandomdev, initstate, setstate - better random number
     generator; routines for changing generators

     Standard C Library (libc, -lc)

     #include <stdlib.h>


While I'm no expert on ANSI standards, I'm pretty sure random() and srandom() are not a part of the standard library. They certainly don't exist in any standard reference I have. They might exist on some platforms, or they might have been added recently.

They also don't exist in Visual C++ .NET. While I know that its not the most standards compliant compiler, it comes pretty close these days. I'd be happy to be corrected if I'm wrong.

I'd find it surprising if the functions that are replacing rand and srand don't behave similarly. rand() only needs to be seeded once.

He is obviously using some other library.  TStringList, AnsiString, and ShowMessage aren't part of the standard library, either.
The problem is like I said before...

     > This is not a proper C++ function call.  It should be:

Without the parens, the compiler is ignoring this function call (I'd be surprised if you didn't at least get a warning), which is causing the random number generator to not be seeded properly, which is causing the same "random" value to come up each time.


TStringList, AnsiString,ShowMessage are in Borland C++ Builder... in whiich case i believe you call:


at least it works for me ;)
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: brettmjohnson {http:#9736977}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.