TIP: Click on subject to list as thread! ANSI
echo: nthelp
to: Geo
from: John Beckett
date: 2004-12-06 12:21:58
subject: Re: c++ help

From: John Beckett 

On Sun, 5 Dec 2004 14:06:01 -0500, "Geo"  wrote:
>> Why not just seed the generator once at program start?

> I tried that but it's a GUI app and the seed doesn't seem to be global to
> the member function that's calling rand().

It's still wrong to call srand every time, just before you need it. Each
thread in your app needs to call srand when the thread is initialised (if
the thread wants to call rand later).

I don't know the proper way to do this in an MFC app. There is sure to be
an init function for this purpose.

One trick is shown below (again, this shows basic stuff that you need to understand).

void CLottoDlg::OnPlay3()
{
  static bool fDoneInit = false;
  if ( !fDoneInit ) {
    fDoneInit = true;
    srand(time(NULL));
  }
  // Code from your function goes here.
}

In the above, "static" means that the variable fDoneInit retains
its value between calls to the function. The result is that srand will only
be called once, the first time OnPlay3 is entered.

If more than one thread called OnPlay3, the above would not be adequate
(each thread has to call srand).

John

--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)
SEEN-BY: 633/267 270 5030/786
@PATH: 379/45 1 396/45 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.