As stated above, you're good to go as long as max % prime != 0:
use strict;
use warnings;
my ($seed, $prime, $max, $input, $result);
$seed = 8;
for $prime (2,3,5,7,11,13,17,19,23) {
for $max (20..30) {
next if $max % $prime == 0;
my %hash;
for $input (0..($max-1)) {
$result = ($prime * $input + $seed) % $max;
$hash{$result}++;
}
print "$prime $max : ";
print join '', values %hash, "\n";
}
}
All you need to guarantee this is a sufficiently large prime so that it's always going to be larger than sqrt(max) (but not equal to max itself).
use strict;
use warnings;
my (@arr, $seed, $max, $prime, $input, $result);
@arr = 1..10;
$seed = 5;
$max = $#arr+1;
$prime = 65521;
for $input (0..($max-1)) {
$result = ($prime * $input + $seed) % $max;
print $arr[$result], " ";
}
The hard part though, since I assume you're doing this in assembly or a low level language, will be making sure that none of the basic mathematical operations go out of bounds.
-
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.