Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: custom random number generator

by TJPride (Pilgrim)
on Nov 29, 2011 at 20:56 UTC ( [id://940698]=note: print w/replies, xml ) Need Help??


in reply to custom random number generator

I think you'll like this. Not only does it randomize your values for you, but it gives a visual display of the result so you can see the distribution. Change the weight to see how it affects things - larger numbers mean closer to $x, smaller means further, with 1 being classic bell curve.

use strict; use warnings; my ($x, $y, $weight, @r) = (5, 10, 2); push @r, multirand($x, $y, $weight) for 1..10000; display(\@r, 50); sub multirand { my ($x, $y, $weight) = @_; return $x * $y ** (rand() ** $weight * (rand() > 0.5 ? 1 : -1)); } sub display { my ($r1, $max) = @_; my (@r2, $skip, $i); $skip = ($#$r1 + 1) / 40; @r2 = sort { $a <=> $b } @$r1; for ($i = 0; $i <= $#r2; $i += $skip) { print 'x' x ($r2[$i] / $max * 80 + 0.5) . "\n"; } }

Output:

x x x xx xx xx xxx xxx xxx xxxx xxxx xxxxx xxxxx xxxxxx xxxxxx xxxxxxx xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found