Forum > Other

Is there a faster random function?

(1/6) > >>

Is there a faster random for Lazarus/fpc than the default random function.
I only need 32bit integers.

I just did some fast pixel stuff, and I was quite impressed with the performance of TLazIntfImage used with GetDataLineStart.
I got  about 50 MPixels/sec ( 2 Ghz Athlon 64 ) 8)

Unfortunately when I wanted to "animate" the pixels with a random colour, performance dropped to 250.000 pix/sec.

This means by rough estimate that random eats about 95% of the processing power! :shock:

I could do a huge array of random numbers, but that seems a bit lame. :oops:

Vincent Snijders:
Using a linear congruential random number generator would probably faster, but those generators are statistically weaker. But maybe that doesn't matter in your case.

It only needs to appear random.
Statistic weakness, missing values, repetition doesn't matter as much as speed.

I've been up all night trying to find something, but I've had no luck.
I thought I could find something meant for games or audio-noise or something where speed is essential.
But I've had no luck so far.

Vincent Snijders:
Inspired by the code from the FASTA benchmark, I give this function for generating a random number between 0..138867.

--- Code: ---
function genRandom: integer;
  seed : integer = 42;
  IM = 139968;
  IA = 3877;
  IC = 29573;
  seed := (seed * IA + IC) mod IM;
  result := seed;
--- End code ---

WOW cool  8)

I didn't know I could make a const, use it ch it and call the function again and reuse the const with the new value.
That's extremely helpful.

The function is very fast, i now get 24 Mpixels/sec
Unfortuinately it creates a clearly wavy pattern, but I think I can fix that.  :D


[0] Message Index

[#] Next page

Go to full version