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

From: "Paul Ranson" 

This is a multi-part message in MIME format.

------=_NextPart_000_00E3_01C4DB04.ABF74E40
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

'rand' returns a pseudo random number between 0 and 32767. So in your =
application 8 and 9 should come up slightly less often than 0-7. To do a =
lotto type thing I think a shuffle is appropriate, luckily C++ contains =
the exact function you want....

#include 

int balls [ 50 ] ;
for ( int i =3D 0; i < 50; ++i )
   balls [ i ] =3D i + 1 ;
std::random_shuffle ( balls, balls + 50 ) ; m_pick4.Format (
"%d-%d-%d-%d", balls [ 0 ], balls [ 1 ], balls [ 2 ], = balls [ 3
]) ;

etc.

I'm pretty sure that that uses the 'rand' function internally, so your =
method for setting the generator would still apply. I'll take a look =
later. This approach also deals with the '0' issue since most lotteries =
choose from 1-n rather than 0-n-1... You can supply your own random =
generator to 'random_shuffle' but this is syntactically a bit trickier.

I found http://www.paradisepoker.com/shuffling.html interesting, if you =
want to do this properly then there are quite a lot of things to think =
about.

Paul

"Geo"  wrote in message news:41b35b61{at}w3.nls.net...
> "Paul Ranson"  wrote in message =
news:41b34f56{at}w3.nls.net...
>=20
>>>I don't think that your 'for i' stuff helps you. Why not just seed =
the
> generator once at program start?
>=20
> 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().
>=20
>>>m_pick3.Format ( "%d-%d-%d", rand () % 10, rand () %
10, rand () % 10 =
) ;
>=20
> Ohhhh.. I like that. Works great for pick3 and pick4 since those are =
all 1
> digit numbers. Now I'm trying to do super lotto which requires 6 two =
digit
> numbers. I can do it the same way
>=20
> m_pick5.Format ("%d-%d-%d-%d-%d", rand()%50, rand()%50, rand()%50,
> rand()%50, rand()%50, rand()%50) ;
>=20
> but that allows duplicates, I could end up with 21-1-21-21-2-21
>=20
> so this one is going to require some checking for duplicates (probably =
using
> variables) before converting them to a string with "-" separators. I'm
> working on that one now but would appreciate any ideas. Your code is =
so much
> more elegant than my brutal stuff.
>=20
> Oh and as for the not being random, it is as long as you can't time =
travel
> or pick twice during the same second..  (well it appears random =
anyway,
> who's to say if anything is *really* random and not just beyond our
> understanding)
>=20
> Geo.
>=20
>
------=_NextPart_000_00E3_01C4DB04.ABF74E40
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable








'rand' returns a pseudo random number between 0 and =
32767. So=20
in your application 8 and 9 should come up slightly less often than 0-7. = To do a=20
lotto type thing I think a shuffle is appropriate, luckily C++ =
contains the=20
exact function you want....
 
#include=20
<algorithm>
 
int balls [ 50 =
]=20
;
for ( int i =3D =
0; i < 50;=20
++i )
   =
balls [ i ] =3D i=20
+ 1 ;
std::random_shuffle ( balls,=20 balls + 50 )
;
m_pick4.Format =
(=20
"%d-%d-%d-%d", balls [ 0 ], balls [ 1 ], balls [ 2 ], balls [ 3 ])=20
;
 
etc.
 
I'm pretty sure that that uses the 'rand' function =
internally,=20
so your method for setting the generator would still apply. I'll take a = look=20
later. This approach also deals with the '0' issue since most lotteries = choose=20
from 1-n rather than 0-n-1... You can supply your own random generator = to=20
'random_shuffle' but this is syntactically a bit trickier.
 
I found http://www.paradisep" target="new">http://www.paradisep=">http://www.paradisepoker.com/shuffling.html">http://www.paradisep=
oker.com/shuffling.html=20
interesting, if you want to do this properly then there are quite a lot = of=20
things to think about.
 
Paul
 
"Geo" <mailto:georger{at}nls.net">
size=3D2>georger{at}nls.net> wrote in message =
news:41b35b61{at}w3.nls.net...> "Paul Ranson" <mailto:paul{at}barkto.com">
size=3D2>paul{at}barkto.com> wrote in message =
news:41b34f56{at}w3.nls.net...>=20
>>>I don't think that your 'for i' stuff
helps you. Why not =
just=20
seed the> generator once at program
start?> > I = tried that=20
but it's a GUI app and the seed doesn't seem to be global
to> the = member=20
function that's calling rand().>
>>>m_pick3.Format ( =

"%d-%d-%d", rand () % 10, rand () % 10, rand () % 10 )
;> =
>=20
Ohhhh.. I like that. Works great for pick3 and pick4 since those are all =

1> digit numbers. Now I'm trying to do super lotto which requires =
6 two=20
digit> numbers. I can do it the same way>
> = m_pick5.Format=20
("%d-%d-%d-%d-%d", rand()%50, rand()%50,
rand()%50,> rand()%50,=20 rand()%50, rand()%50)
;> > but that allows duplicates, I =
could end=20
up with 21-1-21-21-2-21> > so this one is
going to require = some=20
checking for duplicates (probably using> variables) before
= converting=20
them to a string with "-" separators. I'm>
working on that one = now but=20
would appreciate any ideas. Your code is so much> more
elegant = than my=20
brutal stuff.> > Oh and as for the not
being random, it is = as long=20
as you can't time travel> or pick twice during the same second..=20
<g> (well it appears random anyway,> who's to say if =
anything is=20
*really* random and not just beyond our>
understanding)> =
>=20
Geo.> >

------=_NextPart_000_00E3_01C4DB04.ABF74E40--

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