Re: rand() pattern in texture?

From: John M. Gamble (jgamble_at_ripco.com)
Date: 10/11/04


Date: Mon, 11 Oct 2004 21:04:59 +0000 (UTC)

In article <20041010114449.01525.00001787@mb-m17.aol.com>,
Mensanator <mensanator@aol.compost> wrote:
>>Subject: rand() pattern in texture?
>>From: "codemagic" codemagic@gmail.com
>>Date: 10/10/2004 8:24 AM Central Standard Time
>>Message-id: <1097414643.721516.69860@f14g2000cwb.googlegroups.com>
>>
>>
>>Hi,
>>
>>I am generating a texture of 1024 * 1024 pixels. I am using rand() % c
>>to prefill it with some noise before adding some fractal noise.
>>However, perhaps ten percent of the time I am seeing line anomalies in
>>the prefilled texture.
>>
>>Do you think this is due to repetition in the sequence?
>
>Or at least repetion in the portion of the random number that sets the pixel.
>A random number's least significant bits are less reliable than the most
>significant bits.
>
>?Why can I only
>>spot the pattern occasionally? Somebody suggested that not all seeds
>>are equal - is that true? I srand on timeGetTime().
>
>The random number generator has a certain period after which it repeats.
>All a seed does is reposition your starting point within the period. If a
>certain
>area of the period gives you the line anomoly, then there are a million seeds
>that will include that anomoly. If the period is larger than a million, than
>there
>will be seeds that don't include it (although they may show a different
>anomoly).
>
>>
>>Can someone recommend a decent alternative implementation?
>
>Lookup Mersenne Twister. The delelopers of the Python language didn't like
>their original random number generator either and changed it to an
>implementaion
>of Mersenne Twister. Here's what the manual says about it:
>
><quote>
>Python uses the Mersenne Twister as the core generator. It produces 53-bit
>precision floats and has a period of 2**19937-1. The underlying implementation
>in C is both fast and threadsafe. The Mersenne Twister is one of the most
>extensively tested random number generators in existence. However, being
>completely deterministic, it is not suitable for all purposes, and is
>completely
>unsuitable for cryptographic purposes.
></quote>
>
>Do a Google search, you'll find C source code available. I know because I've
>got a copy of it (but don't recall where I got it from). If you can't find it,
>write back.
>
>>Needs to be no slower.
>
>Is a fast random number generator worth it if it produces crappy results?
>Perl seems to be much faster than Python but Perl fails the DieHard test
>whereas Python does not.
>

I don't want to bring a perl/python debate into sci.math, but you're
implying something here that isn't entirely true. Perl the language
is not failing a diehard test (anymore than python the language is
passing it). Their respective libraries use different functions for
randomness, and as you pointed out, perl is using the system rand()
that python once used, which is cheap but not especially good.

But the perl user doesn't have to, as there are alternatives to a
system rand, including a Mersenne Twister if i recall correctly, in
CPAN. You can, i admit, argue that making the default rand() use
the system, er, default, is not the superior choice, but at that
point the "what are you using it for" questions go beyond the scope
of sci.math.

To the original poster: rand(), unless explicitly said otherwise
in the documentation, will usually be a function of barely minimal
usefulness, probably of the form "return (seed = seed * m + c);".

Even that minimal usefulness will be compromised by using a modulo
operator to get the range that you want, because the lower bits are
much more predictable that the higher bits (try modulo 2, for
example).

Mersenne Twister is a good choice for a substitute, not the least
of which is that there is more than one implementation out there
to study.

-- 
	-john
February 28 1997: Last day libraries could order catalogue cards
from the Library of Congress.


Relevant Pages

  • Re: rand() pattern in texture?
    ... Somebody suggested that not all seeds ... The random number generator has a certain period after which it repeats. ... Lookup Mersenne Twister. ... Python uses the Mersenne Twister as the core generator. ...
    (sci.math)
  • Re: Python or PHP?
    ... If it's one of the things for which Python ... every language here and there more ways to do something. ... you make Perl more complicated than it is:-D. ... Not the programmer. ...
    (comp.lang.python)
  • Recommend an E-book Meeting the Following Criteria (Newbie, Long)
    ... I know several programming languages namely Java, Perl and C in this order ... Now I'm wondering which Python book I should get as there are so many out ... I'd like to mostly concentrate on language features but some pointers ...
    (comp.lang.python)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.perl.misc)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.python)

Quantcast