develooper Front page | perl.cvs.parrot | Postings from January 2009

[svn:parrot] r35869 - trunk/languages/perl6/src/classes

From:
coke
Date:
January 21, 2009 09:00
Subject:
[svn:parrot] r35869 - trunk/languages/perl6/src/classes
Message ID:
20090121170024.E4781CB9AE@x12.develooper.com
Author: coke
Date: Wed Jan 21 09:00:22 2009
New Revision: 35869

Modified:
   trunk/languages/perl6/src/classes/Range.pir

Log:
Add a basic version of .pick() for Range and hopefully not break anything.



Modified: trunk/languages/perl6/src/classes/Range.pir
==============================================================================
--- trunk/languages/perl6/src/classes/Range.pir	(original)
+++ trunk/languages/perl6/src/classes/Range.pir	Wed Jan 21 09:00:22 2009
@@ -72,6 +72,29 @@
      .return ($P0)
 .end
 
+=item pick
+
+Select an element at random from the range.
+
+=cut
+
+.sub 'pick' :method
+    .local pmc result, rand
+    rand = get_hll_global ['Any'], '$!random'
+
+    .local int from,to,diff,result
+    from = self.'from'()
+    to = self.'to'()
+    diff = to - from
+
+    $N0 = rand
+    $N0 *= diff
+
+    result = $N0
+    result += from
+ 
+    .return (result)
+.end
 
 =item from()
 



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About