[24665] in Perl-Users-Digest
Perl-Users Digest, Issue: 6829 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 3 14:52:21 2004
Date: Tue, 3 Aug 2004 11:51:38 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 3 Aug 2004 Volume: 10 Number: 6829
Today's topics:
Unix shell interactive with perl - NewBie <leongyc@tp.edu.sg>
Re: Unix shell interactive with perl - NewBie <bernard.el-haginDODGE_THIS@lido-tech.net>
Re: Unix shell interactive with perl - NewBie <matthew.garrish@sympatico.ca>
Re: Unix shell interactive with perl - NewBie <nobull@mail.com>
update a mysql-db with date <larand54@garbidge.netscape.net>
Re: update a mysql-db with date <nobull@mail.com>
Re: update a mysql-db with date <larand54@garbidge.netscape.net>
Re: update a mysql-db with date <nobull@mail.com>
Re: update a mysql-db with date <matthew.garrish@sympatico.ca>
Re: Use a variable value in another variable's name 510046470588-0001@t-online.de
Re: Use a variable value in another variable's name <jurgenex@hotmail.com>
Re: Use a variable value in another variable's name <nobull@mail.com>
Re: Use a variable value in another variable's name <jurgenex@hotmail.com>
Re: Use a variable value in another variable's name <uri@stemsystems.com>
Re: Use a variable value in another variable's name 510046470588-0001@t-online.de
Re: Use a variable value in another variable's name <tadmc@augustmail.com>
Re: Use a variable value in another variable's name <nobull@mail.com>
Re: Use a variable value in another variable's name <nobull@mail.com>
Re: Use a variable value in another variable's name <atp5470 at fsu.edu>
Re: Using rand(), how to I avoid repeats? <usenet@vyznev.invalid>
Re: Using rand(), how to I avoid repeats? ctcgag@hotmail.com
Re: Using rand(), how to I avoid repeats? <Juha.Laiho@iki.fi>
Re: Using rand(), how to I avoid repeats? <Joe.Smith@inwap.com>
Re: Using rand(), how to I avoid repeats? <bowsayge@nomail.afraid.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 23 Jul 2004 17:20:56 +0800
From: "NewBie" <leongyc@tp.edu.sg>
Subject: Unix shell interactive with perl - NewBie
Message-Id: <4100d74f$1@news>
Hi,
How to interact with a unix shell in perl?
Thanks in advance,
YC
------------------------------
Date: Fri, 23 Jul 2004 11:26:13 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: Unix shell interactive with perl - NewBie
Message-Id: <Xns952F74BC9B296elhber1lidotechnet@62.89.127.66>
"NewBie" <leongyc@tp.edu.sg> wrote:
> Hi,
>
> How to interact with a unix shell in perl?
Buy it a drink, compliment it, and for pete's sake, don't make jokes
about its weight...
...or use system() and/or qx//.
--
Cheers,
Bernard
------------------------------
Date: Fri, 23 Jul 2004 09:32:29 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Unix shell interactive with perl - NewBie
Message-Id: <Lr8Mc.13909$Fj.434548@news20.bellglobal.com>
"Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in
message news:Xns952F74BC9B296elhber1lidotechnet@62.89.127.66...
> "NewBie" <leongyc@tp.edu.sg> wrote:
>
> > Hi,
> >
> > How to interact with a unix shell in perl?
>
>
> Buy it a drink, compliment it, and for pete's sake, don't make jokes
> about its weight...
>
>
> ...or use system() and/or qx//.
>
Or read from stdin and print to stdout, if that's what he's after.
Very sage advice that first part, though... : )
Matt
------------------------------
Date: 23 Jul 2004 20:50:39 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Unix shell interactive with perl - NewBie
Message-Id: <u97jsufqpc.fsf@wcl-l.bham.ac.uk>
"Matt Garrish" <matthew.garrish@sympatico.ca> writes:
> "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in
> message news:Xns952F74BC9B296elhber1lidotechnet@62.89.127.66...
> > "NewBie" <leongyc@tp.edu.sg> wrote:
> >
> > > Hi,
> > >
> > > How to interact with a unix shell in perl?
> >
> >
> > Buy it a drink, compliment it, and for pete's sake, don't make jokes
> > about its weight...
> >
> >
> > ...or use system() and/or qx//.
> >
>
> Or read from stdin and print to stdout, if that's what he's after.
>
> Very sage advice that first part, though... : )
vvvvvv
I >>>>Expect<<< the OP was looking for something else :-).
^^^^^^
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 29 Jul 2004 18:57:48 GMT
From: Captain Beefart <larand54@garbidge.netscape.net>
Subject: update a mysql-db with date
Message-Id: <MMbOc.6586$dx3.42066@newsb.telia.net>
Hi,
I have created a database in mysql that have a field in dtetime format.
In perl I have the string "20040728165500" to update this field in the db.
I'm using the module mysql.
Part of the code:
my $sql_statement="INSERT INTO cme (date, name) VALUES ($datetime, $name)" ;
print "$sql_statement\n";
my $sth = $dbh->query($sql_statement);
The output:
INSERT INTO cme (date, name) VALUES (20040729134000, Beefart)
Can't call method "prepare" on an undefined value
at /usr/lib/perl5/vendor_perl/5.8.2/i686-linux/Mysql.pm line 169.
Anyone that can tell me how to do it?
I've been searching for more than a week now. :(
------------------------------
Date: 29 Jul 2004 21:14:37 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: update a mysql-db with date
Message-Id: <u97jsm370y.fsf@wcl-l.bham.ac.uk>
Captain Beefart <larand54@garbidge.netscape.net> writes:
> my $sql_statement="INSERT INTO cme (date, name) VALUES ($datetime, $name)" ;
> print "$sql_statement\n";
> my $sth = $dbh->query($sql_statement);
>
> The output:
> INSERT INTO cme (date, name) VALUES (20040729134000, Beefart)
That doesn't look like valid SQL to me. Not, of course, that this has
anything to do with Perl of course.
You should quote the strings in you SQL or better still use placeholders.
> Can't call method "prepare" on an undefined value
> at /usr/lib/perl5/vendor_perl/5.8.2/i686-linux/Mysql.pm line 169.
Please post a minimal but complete script that reproduces the
symptoms.
This and much other valuable advice can be found in the posting
guidelines.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 30 Jul 2004 10:09:49 GMT
From: Captain Beefart <larand54@garbidge.netscape.net>
Subject: Re: update a mysql-db with date
Message-Id: <N7pOc.99674$dP1.342029@newsc.telia.net>
Brian McCauley wrote:
> Captain Beefart <larand54@garbidge.netscape.net> writes:
>
>> my $sql_statement="INSERT INTO cme (date, name) VALUES ($datetime,
>> $name)" ; print "$sql_statement\n";
>> my $sth = $dbh->query($sql_statement);
>>
>> The output:
>> INSERT INTO cme (date, name) VALUES (20040729134000, Beefart)
>
> That doesn't look like valid SQL to me. Not, of course, that this has
> anything to do with Perl of course.
>
> You should quote the strings in you SQL or better still use placeholders.
>
>> Can't call method "prepare" on an undefined value
>> at /usr/lib/perl5/vendor_perl/5.8.2/i686-linux/Mysql.pm line 169.
>
> Please post a minimal but complete script that reproduces the
> symptoms.
>
> This and much other valuable advice can be found in the posting
> guidelines.
>
Sorry, I found out my mistake by myself, The problem occured already when I
tried to open the database. The opening command was in error.
But it didn't give me any message that this command failed. And as I was
unsure about the dateformat from the beginning and as the error gave me
some indication through the prepare method and that there where some
undefined value, I only could think about this date. Stupid of course
but...
It working now and by the way, you thought it didn't look like valid SQL,
hmm, I think it is and it's working. What was it that you didn't like?
Thank you anyway.
/CB
------------------------------
Date: 30 Jul 2004 18:39:38 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: update a mysql-db with date
Message-Id: <u9pt6dmm1x.fsf@wcl-l.bham.ac.uk>
Captain Beefart <larand54@garbidge.netscape.net> writes:
> Brian McCauley wrote:
>
> > Captain Beefart <larand54@garbidge.netscape.net> writes:
> >> INSERT INTO cme (date, name) VALUES (20040729134000, Beefart)
> >
> > That doesn't look like valid SQL to me.
> It working now and by the way, you thought it didn't look like valid SQL,
SQL doesn't have bareword strings so you need quotes around Beefart.
You probably need them around 20040729134000 too.
> hmm, I think it is and it's working. What was it that you didn't like?
This may mean mysql does have bareword strings. Using bareword
strings, except interactively is a bad idea. That's why we always
recommend that you switch them off in Perl. The same goes for SQL.
It may be handy to have bareword strings in a command line tool but
you shouldn't be using them in programatically generated SQL.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 30 Jul 2004 19:49:18 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: update a mysql-db with date
Message-Id: <_7BOc.29802$BU4.1350374@news20.bellglobal.com>
"Brian McCauley" <nobull@mail.com> wrote in message
news:u9pt6dmm1x.fsf@wcl-l.bham.ac.uk...
> Captain Beefart <larand54@garbidge.netscape.net> writes:
>
> > Brian McCauley wrote:
> >
> > > Captain Beefart <larand54@garbidge.netscape.net> writes:
>
> > >> INSERT INTO cme (date, name) VALUES (20040729134000, Beefart)
> > >
> > > That doesn't look like valid SQL to me.
>
> > It working now and by the way, you thought it didn't look like valid
SQL,
>
> SQL doesn't have bareword strings so you need quotes around Beefart.
> You probably need them around 20040729134000 too.
>
> > hmm, I think it is and it's working. What was it that you didn't like?
>
> This may mean mysql does have bareword strings. Using bareword
> strings, except interactively is a bad idea. That's why we always
> recommend that you switch them off in Perl. The same goes for SQL.
MySQL does not allow bareword strings either on the command line or via DBI
(the string would be treated as the name of a column). If the date field is
a bigint (for example, since I didn't see it specified anywhere what types
he's using) the date he's entering wouldn't cause an error, but the unquoted
string should kill the insert statement. My guess, however, since he wasn't
checking whether the open succeeded is that he's not checking whether the
insert succeeds.
The query() call in his code would also suggest that he's not using the DBI
module, but more likely the Mysql module to access the database, and I'm not
sure if it supports placeholders (one more reason to switch). To the OP,
this doesn't make it any less important to properly quote your variables. At
the very least you should be using the built in quote method. Even if your
code is only run by you, it will be more portable should you decide to
change databases at some future point. And if the variables come from an
untrusted source, it will stop someone from running malicious code on your
database.
Matt
------------------------------
Date: 23 Jul 2004 08:18:37 +0200
From: 510046470588-0001@t-online.de
Subject: Re: Use a variable value in another variable's name
Message-Id: <877jsvz1oi.fsf@debian.i-did-not-set--mail-host-address--so-shoot-me>
"Robin Corcoran" <robinshane@hotmail.com> writes:
> This example shows using a variable name in another variable, which isn't
> what I want to do. I want to use a variable to build up the *name* of
> another variable.
>
> $count=2;
> $Table_$count="Test" #want a var called "Table_2"
>
one may use eval() for this purpose
Klaus Schilling
------------------------------
Date: Fri, 23 Jul 2004 13:46:30 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <WE8Mc.6$PK5.3@nwrddc02.gnilink.net>
510046470588-0001@t-online.de wrote:
> "Robin Corcoran" <robinshane@hotmail.com> writes:
>
>> This example shows using a variable name in another variable, which
>> isn't what I want to do. I want to use a variable to build up the
>> *name* of another variable.
>>
>> $count=2;
>> $Table_$count="Test" #want a var called "Table_2"
>>
> one may use eval() for this purpose
Yes, TIMTOW to shoot yourself in the foot.
You just found another one.
jue
------------------------------
Date: 23 Jul 2004 20:24:26 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <u9smbifrx1.fsf@wcl-l.bham.ac.uk>
"Jürgen Exner" <jurgenex@hotmail.com> writes:
> Robin Corcoran wrote:
> > "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> > news:szQLc.29101$gt1.24406@nwrddc02.gnilink.net...
> >> Robin Corcoran wrote:
> >>> I'm trying to use the value of one variable in the name of another
> >>> variable.
> >>
> >> perldoc -q "variable name"
>
> > This example shows using a variable name in another variable, which
> > isn't what I want to do. I want to use a variable to build up the
> > *name* of another variable.
>
> The difference is irrelevant.
> The FAQ describes the simplified case, where the value of the variable is
> concatenated with nothing to be used as name of the second variable.
> You want to concatenate the value of the first variable with some non-empty
> text to use as the name of the second variable.
>
> There is no difference in how to do that. And much more important: there is
> no difference in why this is A BAD IDEA.
That is untrue, there is a difference. That is to say there is one
additional reason why using data as a whole symref is BAD compared to
using it as part of a symref.
Using (potentially user-supplied) data as a _suffix_ of a variable
name is A BAD IDEA largely for the somewhat intagible reason given in
the FAQ that it "conflates the program-addressable namespace and the
user-addressable one".
To an experienced programmer this feels intuatively bad but actually
it's rather hard to put forward a hard reason for why it is in itself
bad.
The problem of variables accessed by symbolic reference clashing with
ones used in the program is not a real problem because of the prefix.
The problem of being restricted to global variables is not really as
much of an issue as people make out. From a programming point of view
a file-scoped-lexical variable is almost as global as a package-scoped
variable. Yeah, sure package variable _can_ be accessed from outside
their scope using a namespace qualifier but if you put this forward as
an advantage of file-scoped-lexical variable over package-scoped
variables then you are just being silly.
The problem that code using symbolic references is also usually
slightly slower than the equivalent code using a lexically scoped hash
is the main reason not to use symbolic references.
Now if we look at the example in the FAQ of using data as the _whole_
symref then we open up a whole other can of worms with the possibility
of symbolic references stomping all over your symbol table - either by
accident or with malicious intent.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 23 Jul 2004 22:25:09 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <9fgMc.705$qT3.528@nwrddc03.gnilink.net>
Brian McCauley wrote:
> "Jürgen Exner" <jurgenex@hotmail.com> writes:
>> Robin Corcoran wrote:
>>> This example shows using a variable name in another variable, which
>>> isn't what I want to do. I want to use a variable to build up the
>>> *name* of another variable.
>>
>> The difference is irrelevant.
[...]
>> There is no difference in how to do that. And much more important:
>> there is no difference in why this is A BAD IDEA.
>
> That is untrue, there is a difference.
[Long explanation snipped]
While all you wrote makes sense in reality there is pretty much no
difference between if you are run over by a bus or by an 18-wheeler.
So better not to use symbolic references.
jue
------------------------------
Date: Fri, 23 Jul 2004 22:59:31 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <x7oem6z5x3.fsf@mail.sysarch.com>
>>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
BM> The problem that code using symbolic references is also usually
BM> slightly slower than the equivalent code using a lexically scoped
BM> hash is the main reason not to use symbolic references.
small speed differences should never be the main reason to not use a
perl feature. symrefs are bad in so many ways that you don't have to
resort to that reason. my current simple response is that symrefs are
just using the symbol table as a hash tree (with special side effects)
so why no just use a proper hash tree on your own? then you can pass it
around, keep it safe, not worry about side effects, etc.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: 24 Jul 2004 10:33:13 +0200
From: 510046470588-0001@t-online.de
Subject: Re: Use a variable value in another variable's name
Message-Id: <873c3hztx2.fsf@debian.i-did-not-set--mail-host-address--so-shoot-me>
Uri Guttman <uri@stemsystems.com> writes:
>
> small speed differences should never be the main reason to not use a
> perl feature. symrefs are bad in so many ways that you don't have to
> resort to that reason. my current simple response is that symrefs are
> just using the symbol table as a hash tree (with special side effects)
> so why no just use a proper hash tree on your own? then you can pass it
> around, keep it safe, not worry about side effects, etc.
I was suggesting eval (), not symrefs.
Klaus Schilling
------------------------------
Date: Sat, 24 Jul 2004 15:59:31 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <slrncg5jdj.7h7.tadmc@magna.augustmail.com>
510046470588-0001@t-online.de <510046470588-0001@t-online.de> wrote:
> Uri Guttman <uri@stemsystems.com> writes:
> I was suggesting eval (),
There are _two_ eval()s in Perl, "eval EXPR" and "eval BLOCK".
Which were you suggesting?
(your post has expired from my news server)
> not symrefs.
Using symrefs is bad.
Using eval EXPR is even worse.
The usual _good_ solution when you thing you want symrefs is to
chose a better data structure in the first place.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 26 Jul 2004 13:06:07 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <u9k6wrotw0.fsf@wcl-l.bham.ac.uk>
Uri Guttman <uri@stemsystems.com> writes:
> >>>>> "BM" == Brian McCauley <nobull@mail.com> writes:
>
> BM> The problem that code using symbolic references is also usually
> BM> slightly slower than the equivalent code using a lexically scoped
> BM> hash is the main reason not to use symbolic references.
>
> small speed differences should never be the main reason to not use a
> perl feature.
Yes, somehow in my typing and revising of the above sentence the
sentiment got negated. It should, of course, have said "not the main
reason". Opps.
--
( )
. _____[oo
.__/ /\@
. l___ /
# ll ll
###LL LL
------------------------------
Date: 26 Jul 2004 13:06:45 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Use a variable value in another variable's name
Message-Id: <u9hdrvotuy.fsf@wcl-l.bham.ac.uk>
510046470588-0001@t-online.de writes:
> Uri Guttman <uri@stemsystems.com> writes:
> >
> > small speed differences should never be the main reason to not use a
> > perl feature. symrefs are bad in so many ways that you don't have to
> > resort to that reason. my current simple response is that symrefs are
> > just using the symbol table as a hash tree (with special side effects)
> > so why no just use a proper hash tree on your own? then you can pass it
> > around, keep it safe, not worry about side effects, etc.
>
> I was suggesting eval (), not symrefs.
All the arguments against using symrefs inappropriately apply equally,
if not more, to using eval(STRING) to achieve the same effect.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Mon, 26 Jul 2004 11:58:43 -0500
From: "Andrew Palmer" <atp5470 at fsu.edu>
Subject: Re: Use a variable value in another variable's name
Message-Id: <10gadt3nsuqkrf8@corp.supernews.com>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in message
news:Xns952EBF4DAED0Dasu1cornelledu@132.236.56.8...
> "Andrew Palmer" <atp5470 at fsu.edu> wrote in
> news:10g0g1ble97buc5@corp.supernews.com:
> > "Robin Corcoran" <robinshane@hotmail.com> wrote in message
> > news:U6PLc.28969$Gf7.1088179@news20.bellglobal.com...
>
> >> I want to use the value of that variable in creating another
> >> variable.
> ...
> > Well, you don't want to use "my." And I hope you're not using
> >
> > use strict;
> >
> > either because that will mess everything up.
>
> May I suggest considering not admitting to the world that you do not
>
> use strict;
>
> in your scripts?
lol. Someone took the bait. ;)
>
> > Try this:
> >
> > $tableCount=2;
> > ${"table_${tableCount}_cols"}="5";
> > print $table_2_cols; # prints 5
In all seriousness, there's never a circumstance when you need to do the
above.
use strict; # yes, this is a good idea
use warnings; # so is this, by the way
my @table_cols;
my $tableCount=2;
$table_cols[$tableCount]="5";
print $table_cols[2]; # prints 5
>
> Please do not do this. Have you read the other responses to this request
> and looked at the documents provided? What you are doing is a BAD IDEA.
>
> http://perl.plover.com/varvarname.html
> http://perl.plover.com/varvarname2.html
> http://perl.plover.com/varvarname3.html
>
> perldoc -q "variable name"
>
> Sheeesh!
> --
> A. Sinan Unur
> 1usa@llenroc.ude.invalid
> (remove '.invalid' and reverse each component for email address)
>
------------------------------
Date: Thu, 22 Jul 2004 15:45:46 +0300
From: Ilmari Karonen <usenet@vyznev.invalid>
Subject: Re: Using rand(), how to I avoid repeats?
Message-Id: <slrncfvdnq.15s.usenet@yhteiskone.vyznev.net>
On 2004-07-21, Scott Stark <sstark@us.ibm.com> wrote:
> I'm using rand() to generate a series of random numbers. Is there any
> way to prevent the same number from being used more than once? All I
> can think of is keeping a list of used numbers, and having some kind
> of "if used, next" statement, which seems lame. (Theoretically that
> could keep going forever, if it randomly kept selecting the same
> numbers.) Any thoughts?
Several solutions have been presented already. Here's one I haven't
seen posted in this thread yet: a "lazy sparse shuffle".
my %int_pool;
use constant MAX_UNIQUE_INT => 2**32-1;
sub get_unique_int () {
my $n = keys %int_pool;
my $r = $n + int rand(MAX_UNIQUE_INT+1 - $n);
my $o = (exists $int_pool{$r} ? $int_pool{$r} : $r);
$int_pool{$r} = (exists $int_pool{$n} ? $int_pool{$n} : $n);
return $o;
}
This method works the like the hash solutions already posted, except
that it has a guaranteed upper bound for execution time, even if the
number space does get densely sampled. On the other hand, it only
works for sample spaces that can be easily enumerated.
--
Ilmari Karonen
If replying by e-mail, please replace ".invalid" with ".net" in address.
------------------------------
Date: 24 Jul 2004 16:12:52 GMT
From: ctcgag@hotmail.com
Subject: Re: Using rand(), how to I avoid repeats?
Message-Id: <20040724121252.647$bR@newsreader.com>
Juha Laiho <Juha.Laiho@iki.fi> wrote:
> ctcgag@hotmail.com said:
> >sstark@us.ibm.com (Scott Stark) wrote:
> >> All I can think of is keeping a list of used numbers, and having some
> >> kind of "if used, next" statement, which seems lame.
> ...
> >> (Theoretically that could keep going forever, if it randomly kept
> >> selecting the same numbers.) Any thoughts?
> >
> >If your random number generator degenerates thus, then in that case you
> >are pretty much screwed regardless.
>
> Not so -- think of a situation where your number space is f.ex.
> 1000 numbers, and you don't want repetitions. You'll end up with a
> significant and inreasing possibility of retries for each new number.
Increasing, but for the most part trivial.
> For the last number, there's only a 1/1000 chance that the random
> algorithm comes up with the desired number. I made a small script
> to test this; even with range of 3 (create randomised list of numbers
> of 1 to 3) I got with short testing 12 "false guesses" in generating
> the list. With larger ranges the number of false guesses got
> significantly worse -- with just range of 1000, the worst results I got
> had over 3000000 false guesses. Results with less than 1000000 false
> guesses seemed to be rare.
You clearly did something wrong. It rarely takes over 15,000 tries to get
all numbers from 1 to 1,000. The odds against needing 1,000,000 tries are
beyond astronomical.
If your random number generator is so defective that you can't get the
numbers you need from it using the hash method, then neither can you trust
its use in your shuffling procedure.
>
> So, for a list of random numbers without repetitions, the shuffled list
> algorithms are definitely better.
That is true as long as it is possible to enumerate all possible numbers
within the domain and you want to sample a large fraction of that domain
(as I said myself in the paragraph you snipped).
If we are talking about general methods, I'd rather have a method that
works in general and is slightly suboptimal in a special case, than a
method that fails in general but works in one special case.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Sun, 25 Jul 2004 09:02:02 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Using rand(), how to I avoid repeats?
Message-Id: <cdvsqt$7qj$1@ichaos.ichaos-int>
ctcgag@hotmail.com said:
>Juha Laiho <Juha.Laiho@iki.fi> wrote:
>> with just range of 1000, the worst results I got had over 3000000
>> false guesses. Results with less than 1000000 false guesses seemed to
>> be rare.
>
>You clearly did something wrong. It rarely takes over 15,000 tries to get
>all numbers from 1 to 1,000. The odds against needing 1,000,000 tries are
>beyond astronomical.
Thanks for the correction; my previous test snippet apparently did have
some dire problem -- now I seem to get repetitions in the range of 5000
when generating numbers 1..1000 - so looks like I can agree with your
results.
Apologies for criticizing your method on false grounds.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
------------------------------
Date: Sun, 25 Jul 2004 09:22:37 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Using rand(), how to I avoid repeats?
Message-Id: <wZKMc.183105$XM6.78082@attbi_s53>
Juha Laiho wrote:
> Not so -- think of a situation where your number space is f.ex.
> 1000 numbers, and you don't want repetitions. You'll end up with a
> significant and inreasing possibility of retries for each new number.
> For the last number, there's only a 1/1000 chance that the random
> algorithm comes up with the desired number.
If you have 1000 numbers available, and have used 999 of them,
then the last number is *NOT* random! Using rand() to return
the one and only possible number is not the way to do things.
Use shuffle algorithm instead.
-Joe
------------------------------
Date: Tue, 27 Jul 2004 05:48:36 GMT
From: bowsayge <bowsayge@nomail.afraid.org>
Subject: Re: Using rand(), how to I avoid repeats?
Message-Id: <U0mNc.15718$f4.12100@newsread3.news.atl.earthlink.net>
Joe Smith said to us:
>
> If you have 1000 numbers available, and have used 999 of them,
> then the last number is *NOT* random! Using rand() to return
> the one and only possible number is not the way to do things.
> Use shuffle algorithm instead.
> -Joe
Bowsayge tested both the random algorithm (using a hash to avoid dups)
and the shuffle algorithm, and he found that the random algorithm is
better when there is a large range (relative to the number of results
wanted).
IOW, if he wants to get 1000 random numbers which range from 1 to 1
million, random (with hash) is pretty darn good.
But if he wants to get 1000 random numbers which range from 1 to 1000,
the random algorithm makes seven times as many collisions as valid results,
so the shuffle algorithm greatly outperforms it.
However, shuffle is not perfect, because it requires a lot of memory for
big ranges. What if only 50 unique random numbers are required, but the
range is 1 to 10 million?
This simply reinforces what Bowsayge was taught in school: algorithms
have to be chosen for data sets.
--
bowsayge
bow-say-ge?
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 6829
***************************************