TIP: Click on subject to list as thread! ANSI
echo: nthelp
to: Geo
from: Paul Ranson
date: 2004-12-05 18:20:42
subject: Re: c++ help

From: "Paul Ranson" 

This is a multi-part message in MIME format.

------=_NextPart_000_002B_01C4DAF7.220CAD90
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I don't think that your 'for i' stuff helps you. Why not just seed the =
generator once at program start?

Assuming 'm_pick3' is a CString you could write,

m_pick3.Format ( "%d-%d-%d", rand () % 10, rand () % 10, rand () % 10 ) =
;

in place of most of your function.

So maintaining your random number of calls to rand,

void CLottoDlg::OnPlay3()
{
   srand(time(NULL));
   for (int i=3D1; i<=3Drand(); i++)
   {
   }
   m_pick3.Format ( "%d-%d-%d", rand () % 10, rand () % 10, rand () % 10 =
) ;
   UpdateData(FALSE);
}

There's no need to do your formatting each time around the 'for' loop, =
you just want to call 'rand' a number of times, so an empty loop will be =
faster. You could consider making a 'MyRandomSeed' function that = prepares
the generator for use, then you can write a 'OnPlay4' function = without
having to cut and paste the code.

But what you wrote will, I think, work...

(Except that I think 'rand () % 10' isn't random.)

Paul

"Geo"  wrote in message
news:41b33c2c$1{at}w3.nls.net...
> "John Beckett" 
wrote in message
> news:41b2a4bc.23977267{at}216.144.1.254...
>=20
>> OMG! Please read this slowly -- a lot of details!
>=20
> Ok first a quick explanation, We haven't been shown itoa function and =
the
> book has minimal information about it (like that it exists and here is =
the
> format with no explanation) so I was just guessing on how to use it.
>=20
> Here is what I came up with:
>=20
> void CLottoDlg::OnPlay3()
> {
> char s[10], a[2], b[2], c[2];
> srand(time(NULL));
> for (int i=3D1; i<=3Drand(); i++)
> {
>  itoa((rand()%10),a,10);
>  itoa((rand()%10),b,10);
>  itoa((rand()%10),c,10);
>  s[0]=3Da[0];
>  s[1]=3D'-';
>  s[2]=3Db[0];
>  s[3]=3D'-';
>  s[4]=3Dc[0];
>  s[5]=3D'\0';
>=20
>  m_pick3=3Ds;
> }
> UpdateData(FALSE);
> }
>=20
> this builds the string 4-2-7 or whatever the random function picks for =
the 3
> digits, it does it in a way that at least "appears" to be pretty =
random
> (that's why I use the i<=3Drand() in the loop) and it's displaying it
> correctly in the window of my gui program.
>=20
> It may not be elegant code, but the trick is not to make the code look =
good,
> the trick is to pick the correct 3 numbers for the lottery. 
>=20
> Ok, on to pick4 :) I'll post a version of the exe when I'm done.
>=20
> Geo.  addressed your concerns without changing my method>
>=20
>
------=_NextPart_000_002B_01C4DAF7.220CAD90
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable








I don't think that your 'for i' stuff helps
you. Why =
not just=20
seed the generator once at program start?
 
Assuming 'm_pick3' is a CString you could =
write,
 
m_pick3.Format =
( "%d-%d-%d",=20
rand () % 10, rand () % 10, rand () % 10 ) ;
 
in place of most of your
function.
 
So maintaining your random number of calls to=20
rand,
 
void=20
CLottoDlg::OnPlay3(){   =
srand(time(NULL));
   =
for (int i=3D1;=20
i<=3Drand(); i++)  
{   }

   =
m_pick3.Format (=20
"%d-%d-%d", rand () % 10, rand () % 10, rand () % 10 ) =
;  =20
UpdateData(FALSE);
}
 
There's no need to do your formatting each time around =
the 'for'=20
loop, you just want to call 'rand' a number of times, so an empty loop = will be=20
faster. You could consider making a 'MyRandomSeed' function that = prepares the=20
generator for use, then you can write a 'OnPlay4' function without = having to cut=20
and paste the code.
 
But what you wrote will, I think, =
work...
 
(Except that I think 'rand () % 10' isn't=20
random.)
 
Paul
 
"Geo" <mailto:georger{at}nls.net">
size=3D2>georger{at}nls.net> wrote in message =
news:41b33c2c$1{at}w3.nls.net...> "John Beckett" <mailto:FirstnameSurname{at}compuserve.com.omit">
size=3D2>FirstnameSurname{at}compuserve.com.omit> wrote in=20
message> news:41b2a4bc.23977267{at}216.144.1.254...>=20
>> OMG! Please read this slowly -- a lot of
details!> =
>=20
Ok first a quick explanation, We haven't been shown itoa function and=20
the> book has minimal information about it (like that it
exists = and here=20
is the> format with no explanation) so I was just guessing
on how = to use=20
it.> > Here is what I came up
with:> > void=20
CLottoDlg::OnPlay3()> {> char
s[10], a[2], b[2],=20
c[2];> srand(time(NULL));> for
(int i=3D1; = i<=3Drand();=20
i++)> {> 
itoa((rand()%10),a,10);> =20
itoa((rand()%10),b,10);>  =
itoa((rand()%10),c,10);> =20
s[0]=3Da[0];> 
s[1]=3D'-';>  =
s[2]=3Db[0];> =20
s[3]=3D'-';> 
s[4]=3Dc[0];>  =
s[5]=3D'\0';>=20
>  =
m_pick3=3Ds;> }> UpdateData(FALSE);>=20
}> > this builds the string 4-2-7 or
whatever the random =
function=20
picks for the 3> digits, it does it in a way that at least
= "appears" to=20
be pretty random> (that's why I use the i<=3Drand()
in the = loop) and=20
it's displaying it> correctly in the window of my gui =
program.>=20
> It may not be elegant code, but the trick is not to make the =
code look=20
good,> the trick is to pick the correct 3 numbers for the
= lottery.=20
<g>> > Ok, on to pick4 :)
I'll post a version of the =
exe=20
when I'm done.> > Geo. <let me
know if you see any = technical=20
errors in the above, I think I> addressed your concerns
without = changing=20
my method>>
>

------=_NextPart_000_002B_01C4DAF7.220CAD90--

--- 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™.