Solved

Problems with random() function

Posted on 2003-11-12
8
3,074 Views
Last Modified: 2010-04-01
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);

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

  ShowMessage(FileName);
0
Comment
Question by:kabix
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9735975
kabix:

>   Randomize;

This is not a proper C++ function call.  It should be:
     Randomize();


Hope that helps,
Dex*
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 20 total points
ID: 9735980
shouldn't you be calling

// generate random seed
   randomize();

instead of Randomize; ??
0
 
LVL 3

Expert Comment

by:Kashra
ID: 9736435
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!
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9736977
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:

srandom(time(NULL));

...

long num = random();

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

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

DESCRIPTION
     These interfaces are obsoleted by random(3).



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

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdlib.h>

....

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:Kashra
ID: 9737092
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.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9740994
Guys,

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:
          Randomize();

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.

Dex*

0
 
LVL 18

Expert Comment

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

randomize();

at least it works for me ;)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249093
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now