Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

RandomPatterForPerlinNoise

using System.IO;
using System;

class Program
{
    static void Main()
    {
        //Random random = new Random();
        //int rnd = random.Next(256);
        //Console.WriteLine("random: " + rnd);
        
        RandomPatter rp = new RandomPatter();
        rp.generateShufflePatter();
        rp.printPatter(16, 16);
    }
}

class RandomPatter
{
    private int m_size;
    private int[] m_sortedPatter;
    private int[] m_randomPatter;
    
    public RandomPatter()
    {
        m_size = 256;
        m_sortedPatter = new int[m_size];
        m_randomPatter = new int[m_size];
    }
    
    
    private int[] generateSortedPatter()
    {
        for(int i=0; i<m_size; i++)
        {
            m_sortedPatter[i] = i;
        }
        
        return m_sortedPatter;
    }
    
    
    public int[] generateShufflePatter()
    {
        m_sortedPatter = generateSortedPatter();
        
        for(int i=0; i<m_size; i++)
        {
            m_randomPatter[i] = m_sortedPatter[i];
        }

        for(int i=0; i<100; i++)
        {
            //int rand1 = Random.Range(0, 256);
            //int rand2 = Random.Range(0, 256);
            Random rand = new Random();
            int rand1 = (int)(rand.NextDouble() * 256);
            int rand2 = (int)(rand.NextDouble() * 256);

            int toggle = m_randomPatter[rand1];
            m_randomPatter[rand1] = m_randomPatter[rand2];
            m_randomPatter[rand2] = toggle;
        }
        
        return m_randomPatter;
    }
    

    public void printPatter(int w, int h)
    {
        int i = 0;
        for(int y=0; y<h; y++)
        {
            for(int x=0; x<w; x++)
            {
                if(m_randomPatter[i] < 10)
                {
                    Console.Write("  ");
                }
                else if(m_randomPatter[i] < 100)
                {
                    Console.Write(" ");
                }
                
                Console.Write(m_randomPatter[i++]);
                Console.Write(" ");
            }
            
            Console.WriteLine("");
            
        }
    }
    
    
    
    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.