So, I wrote a reply: Re: Make random numbers for fun, that generates random numbers using Inline::C, but afterwards realized that the parameters to my C function were always the first three elements of the stack ("return" of the array of random nums).
Here's the code, and the sample output. All other numbers in the list are random as expected:
use warnings;
use strict;
use Inline 'C';
my @random_numbers = getRand(1, 1000, 100);
print "$_\n" for @random_numbers;
__END__
__C__
#include <stdlib.h>
void getRand (int start, int end, int iterations){
time_t t;
srand((unsigned) time(&t));
inline_stack_vars;
int i;
for (i = 0; i < iterations; i++){
int randomNum = rand() % (end - start);
inline_stack_push(sv_2mortal(newSViv(randomNum)));
}
inline_stack_done;
}
Output:
1
1000
100
...
I can't seem to find anything in the docs on how to avoid this. What's causing the parameters to be at the forefront of the stack?