PhilMurwin / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

Add support for enumerable noise sets #21

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
A useful method to perform algorithms that use noise would be to set up the
noise data as an enumeration, much like Enumerable.Range(int start, int
count). The proposed method would be Enumerable.Noise(int seed), yielding
infinite procedural noise generation. 

Then, to take five random numbers one would call var randomNumbers =
Enumerable.Noise(seed).Take(5); 

There is alot of uses for floating point noise aswell, mainly in procedural
fractal generation. 

Enumerable.Noise<float>(seed); // Generate noise between 0-1
Enumerable.NoiseSigned<float>(seed); // Generate noise between (-1)-1

Due to the problem of infinite enumeration, foreach loops on raw
enumerations of noise would be problematic. Perhaps one should set up a
maximum range of numbers?

Original issue reported on code.google.com by l.jarven...@gmail.com on 25 Mar 2009 at 2:40

GoogleCodeExporter commented 9 years ago

Original comment by azizatif on 4 Apr 2009 at 6:30

GoogleCodeExporter commented 9 years ago
What about:

public static IEnumerable<TReturn> Infinity<TReturn>(TReturn start, 
  Func<TReturn, TReturn?> generator)
  where TReturn : struct 
{
  generator.ThrowIfNull("generator");
  TReturn? val = start;
  while (val.HasValue)
  {
    yield return val.Value;
    val = generator(val.Value);
  }
}

public static IEnumerable<double> Noise(int seed)
{
  Random r = new Random(seed);
  return Infinity(r.NextDouble(), x => r.NextDouble());
}

Original comment by bruno.ac...@gmail.com on 22 Oct 2011 at 4:02

GoogleCodeExporter commented 9 years ago
mail me if you want the full implementation with testing

Original comment by bruno.ac...@gmail.com on 22 Oct 2011 at 5:17

GoogleCodeExporter commented 9 years ago
That's a pretty easy way of doing it, yes. Coming to think about it again after 
these years, maybe it's a bit odd to have an infinite enumerable? I haven't 
found other places in .NET where an enumerable sequence never end. 

Original comment by l.jarven...@gmail.com on 22 Oct 2011 at 11:15