Well, I don't know if this is interesting, but here's a patch that implement: op rand(out NUM) op rand(out NUM, in INT)
and does what you can guess. There's also a test file (I don't know wether the tests should go in an existant test file). But, there is a tiny, little problem: I don't call srand(), and thus get always the same value. I don't know *where* to call srand(). I don't even know *if* I should call srand(). Anyway, here's the patch and be kind since this is my first patch ;) (pod cleaning doesn't count). Please, could you tell me if the parrot team (that is, all of you guys) wants to add new ops, or if we should wait for a new source organization / extension API / etc. This makes me think: I looked for the ord() and chr() functions, and found just the ord() in core.ops, that calls string_ord() in string.c. I wanted to code the chr() function, but I realized that it depends in fact from the encoding? How one should treat this? Jerome -- [EMAIL PROTECTED]
diff -urbN parrot.orig/core.ops parrot/core.ops --- parrot.orig/core.ops Mon Aug 12 17:00:08 2002 +++ parrot/core.ops Tue Aug 13 23:22:21 2002 @@ -4055,6 +4055,30 @@ ######################################## +=item B<rand>(out NUM) + +Put a fractionnal random value greater than or equal to "0" and less +than 1 into $1. + +=item B<rand>(out NUM, in INT) + +Put a random fractional number greater than or equal to "0" and less +than $2 into $1. ($2 should be positive.) + +=cut + +inline op rand(out NUM) { + $1 = (FLOATVAL) rand() / ((FLOATVAL)RAND_MAX+1); + goto NEXT(); +} + +inline op rand(out NUM, in INT) { + $1 = (FLOATVAL) rand() / ((FLOATVAL)RAND_MAX+1) * $2; + goto NEXT(); +} + +######################################## + =item B<sleep>(in INT) Sleep for $1 seconds. diff -urbN parrot.orig/t/op/rand.t parrot/t/op/rand.t --- parrot.orig/t/op/rand.t Thu Jan 1 01:00:00 1970 +++ parrot/t/op/rand.t Tue Aug 13 23:19:42 2002 @@ -0,0 +1,45 @@ +#! perl -w + +use Parrot::Test tests => 2; + +output_is(<<'CODE', <<'OUTPUT', "rand"); + rand N0 + ge N0, 0.0, OK1 + branch FAIL +OK1: print "ok, rand is greater or equal than 0.0\n" + lt N0, 1.0, OK2 + branch FAIL +OK2: print "ok, rand is less than 1.0\n" + branch OK_ALL +FAIL: print "failure\n" + print "N0 was: " + print N0 + print "\n" +OK_ALL: + end +CODE +ok, rand is greater or equal than 0.0 +ok, rand is less than 1.0 +OUTPUT + +output_is(<<'CODE', <<'OUTPUT', "rand_n"); + rand N0, 10 + ge N0, 0.0, OK1 + branch FAIL +OK1: print "ok, rand is greater or equal than 0.0\n" + lt N0, 10.0, OK2 + branch FAIL +OK2: print "ok, rand is less than 10.0\n" + branch OK_ALL +FAIL: print "failure\n" + print "N0 was: " + print N0 + print "\n" +OK_ALL: + end +CODE +ok, rand is greater or equal than 0.0 +ok, rand is less than 10.0 +OUTPUT + +1;