Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day BrowserUk,

"But the worse case scenarios for the algorithm are when you get a bunch of points grouped in one place and then a few outliers far away. Either in another bunch, or widespread."

Perhaps the following might be suitable. The "bunch of points grouped in one place" tend towards the centre of the plane; the outliers are typically widespread rather than forming outlying clumps.

#!/usr/bin/env perl use strict; use warnings; my $rand_bias; # Boolean (for testing) my ($x_high, $y_high) = (80, 80); my $N = 10_000; for my $rand_bias_bool (0, 1) { $rand_bias = $rand_bias_bool; print '*** ', $rand_bias ? 'WITH' : 'NO', ' RANDOM BIAS', " ***\n" +; my @points = map [ gen_rand($x_high), gen_rand($y_high) ], 1 .. $N +; print_matrix(\@points); } sub gen_rand { my $rand_high = shift; return int rand $rand_high unless $rand_bias; # For testing only! my $high_part = $rand_high; my $rand_sum = 0; while ($high_part) { my $rand_arg = 1 + int rand $high_part; $high_part -= $rand_arg; $rand_sum += int rand $rand_arg; } return $rand_sum; } sub print_matrix { my $coords = shift; my %matrix; for (@$coords) { my ($x, $y) = @$_; ++$matrix{$x}{$y}; } for my $y (reverse 0 .. $y_high - 1) { for my $x (0 .. $x_high - 1) { if (exists $matrix{$x}{$y}) { $matrix{$x}{$y} = '@' if $matrix{$x}{$y} > 9; } else { $matrix{$x}{$y} = ' '; } print $matrix{$x}{$y}; } print "\n"; } }

Here's a sample run showing an (ASCII) graphical representation of the generated data. Note that I've only used an 80x80 grid (rather than your posted 500x500) to get a reasonable display.

Update: I removed two lines with $loops which I'd used for testing but aren't part of the solution.

-- Ken


In reply to Re: Randomly biased, random numbers. by kcott
in thread Randomly biased, random numbers. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found