Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: generate random number without using any built in function in perl?

by tobyink (Abbot)
on May 08, 2013 at 21:27 UTC ( #1032680=note: print w/ replies, xml ) Need Help??


in reply to generate random number without using any built in function in perl?

OK, here's an example. It's not anything like a useful random number generator for cryptographic purposes, but looks fairly random at a first glance...

use v5.12; { my $seed = time; sub myrand { do { $seed *= $seed; $seed =~ s/[^0-9]//g; my $midpoint = int(length($seed) / 2); $seed = substr($seed, 0, $midpoint) . substr($seed, $midpo +int); $seed = substr($seed, 0, 9); } until ($seed > 10_000_000); my $r = reverse substr($seed, 0, 6); my $lim = @_ ? shift : 1; $lim * ($r/1_000_000); } } # 50 random numbers less than 10... say myrand(10) for 1..50;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: generate random number without using any built in function in perl?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032680]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-25 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls