Skip to main content
Readded context after code roll back
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

So I wrote this codewanted to generatecode a pseudo random number based on an integer. I want to use itsimple PRNG for generating terrain in a game. Someone said I should try out hashing so that's what I did. The code is short and works but I'm pretty sure there isIt has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a better way. I'mpseudo random number

2 as input (x) gives 2 as a beginner so any tips are welcome.pseudo random number

So I wrote this code to generate a pseudo random number based on an integer. I want to use it for generating terrain in a game. Someone said I should try out hashing so that's what I did. The code is short and works but I'm pretty sure there is a better way. I'm a beginner so any tips are welcome.

So I wanted to code a simple PRNG for generating terrain in a game. It has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a pseudo random number

2 as input (x) gives 2 as a pseudo random number

Rollback to Revision 4
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

So I wanted towrote this code to generate a simple PRNGpseudo random number based on an integer. I want to use it for generating terrain in a game. It has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a pseudo random number

2 as input (x) gives 2 as a pseudo random number

Here's the improved version with more readable variablesSomeone said I should try out hashing so that's what I did. Also,The code is short and works but I'm pretty sure there is a better solution that fits all my requirements?way. I'm a beginner so any tips are welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int xi0 = 0; 


            while (true)
            {
                x++;i0++;                
                Console.WriteLine(x + " as input gives "+ RandomHashFunction(xi0, 10));
                //returns random number between 0 and max based on input
            }
        } 

        static int RandomHashFunction(int input, int max)
        {
            using (var sha1SHA1 = new SHA1CryptoServiceProvider()) // Disposing things is a good habit;
            {
  StringBuilder sb = new StringBuilder();
          var ascii =string ASCIIEncoding.ASCII;hash2string;
            double output;
   var hashAsString = ascii.GetChars(
       byte[] hash;
            sha1SHA1.ComputeHash(ASCIIEncoding.ASCII.GetBytes(input.ToString()));
            hash = SHA1.Hash;
          ascii.GetBytes(input  sb.ToStringClear());
     
            foreach (byte b in hash)
            {
    );
            sb.Append(b.ToString());
     return Convert.ToInt32(
      }
            hash2string = Mathsb.AbsToString();
            output = hash2string.GetHashCode();
            output = Math.IEEERemainder(hashAsString.GetHashCode()output, max);
                  output = Math.Abs(output);
                );
            }return Convert.ToInt16(output);
        }
    }
}

So I wanted to code a simple PRNG for generating terrain in a game. It has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a pseudo random number

2 as input (x) gives 2 as a pseudo random number

Here's the improved version with more readable variables. Also, is there a better solution that fits all my requirements?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            while (true)
            {
                x++;                
                Console.WriteLine(x + " as input gives "+ RandomHashFunction(x, 10));
                //returns random number between 0 and max based on input
            }
        }
        static int RandomHashFunction(int input, int max)
        {
            using (var sha1 = new SHA1CryptoServiceProvider()) // Disposing things is a good habit
            {
                var ascii = ASCIIEncoding.ASCII;
                var hashAsString = ascii.GetChars(
                    sha1.ComputeHash(
                        ascii.GetBytes(input.ToString())
                    )
                );
                return Convert.ToInt32(
                    Math.Abs(
                        Math.IEEERemainder(hashAsString.GetHashCode(), max)
                    )
                );
            }
        }
    }
}

So I wrote this code to generate a pseudo random number based on an integer. I want to use it for generating terrain in a game. Someone said I should try out hashing so that's what I did. The code is short and works but I'm pretty sure there is a better way. I'm a beginner so any tips are welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int i0 = 0; 


            while (true)
            {
                i0++;                
                Console.WriteLine(RandomHashFunction(i0, 10));
                //returns random number between 0 and max based on input
            }
        } 

        static int RandomHashFunction(int input, int max)
        {
            var SHA1 = new SHA1CryptoServiceProvider();
            StringBuilder sb = new StringBuilder();
            string hash2string;
            double output;
            byte[] hash;
            SHA1.ComputeHash(ASCIIEncoding.ASCII.GetBytes(input.ToString()));
            hash = SHA1.Hash;
            sb.Clear();
 
            foreach (byte b in hash)
            {
                sb.Append(b.ToString());
            }
            hash2string = sb.ToString();
            output = hash2string.GetHashCode();
            output = Math.IEEERemainder(output, max);
            output = Math.Abs(output);
            
            return Convert.ToInt16(output);
        }
    }
}
added improved code and samples, also better explanation of what I wanted to achieve
Source Link
Wagacca
  • 179
  • 1
  • 1
  • 7

So I wrote this codewanted to generatecode a pseudo random number based on an integer. I want to use itsimple PRNG for generating terrain in a game. Someone said I should try out hashing so that's what I didIt has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a pseudo random number

2 as input (x) gives 2 as a pseudo random number

Here's the improved version with more readable variables. The codeAlso, is short and works but I'm pretty sure there is a better way. I'm a beginner so any tips are welcome.solution that fits all my requirements?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int i0x = 0;

 
            while (true)
            {
                i0++;x++;                
                Console.WriteLine(x + " as input gives "+ RandomHashFunction(i0x, 10));
                //returns random number between 0 and max based on input
            }
        }
 
        static int RandomHashFunction(int input, int max)
        {
            using (var SHA1sha1 = new SHA1CryptoServiceProvider();
) // Disposing things is a good habit
     StringBuilder sb = new StringBuilder();   {
            string hash2string;   var ascii = ASCIIEncoding.ASCII;
            double output;
   var hashAsString = ascii.GetChars(
       byte[] hash;
            SHA1sha1.ComputeHash(ASCIIEncoding.ASCII.GetBytes(input.ToString()));
            hash = SHA1.Hash;
            sbascii.ClearGetBytes(input.ToString();
)
            foreach (byte b in hash    )
            {
     );
           sb.Append(b.ToString());
     return Convert.ToInt32(
      }
            hash2string = sbMath.ToStringAbs();
            output = hash2string.GetHashCode();
            output = Math.IEEERemainder(outputhashAsString.GetHashCode(), max);
            output = Math.Abs(output      );
            
     );
        return Convert.ToInt16(output);   }
        }
    }
}

So I wrote this code to generate a pseudo random number based on an integer. I want to use it for generating terrain in a game. Someone said I should try out hashing so that's what I did. The code is short and works but I'm pretty sure there is a better way. I'm a beginner so any tips are welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int i0 = 0;

 
            while (true)
            {
                i0++;                
                Console.WriteLine(RandomHashFunction(i0, 10));
                //returns random number between 0 and max based on input
            }
        }
 
        static int RandomHashFunction(int input, int max)
        {
            var SHA1 = new SHA1CryptoServiceProvider();
            StringBuilder sb = new StringBuilder();
            string hash2string;
            double output;
            byte[] hash;
            SHA1.ComputeHash(ASCIIEncoding.ASCII.GetBytes(input.ToString()));
            hash = SHA1.Hash;
            sb.Clear();

            foreach (byte b in hash)
            {
                sb.Append(b.ToString());
            }
            hash2string = sb.ToString();
            output = hash2string.GetHashCode();
            output = Math.IEEERemainder(output, max);
            output = Math.Abs(output);
            
             return Convert.ToInt16(output);
        }
    }
}

So I wanted to code a simple PRNG for generating terrain in a game. It has to have the following properties:

  1. Computationally cheap

  2. Generate semi random numbers between 0 and an positive integer.

  3. I need to be able to go back and teleport anywhere in the world so random.Next doesn't cut it.

  4. I basically want a noise function. One output for each input with repeatable results.

  5. No seed will be used. If I need another series I'll just shift x value by a couple of millions

These are some outputs produced by my program:

1 as input (x) gives 2 as a pseudo random number

2 as input (x) gives 2 as a pseudo random number

Here's the improved version with more readable variables. Also, is there a better solution that fits all my requirements?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Security.Cryptography;

namespace Hashing_Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            while (true)
            {
                x++;                
                Console.WriteLine(x + " as input gives "+ RandomHashFunction(x, 10));
                //returns random number between 0 and max based on input
            }
        }
        static int RandomHashFunction(int input, int max)
        {
            using (var sha1 = new SHA1CryptoServiceProvider()) // Disposing things is a good habit
            {
                var ascii = ASCIIEncoding.ASCII;
                var hashAsString = ascii.GetChars(
                    sha1.ComputeHash(
                        ascii.GetBytes(input.ToString())
                    )
                );
                return Convert.ToInt32(
                    Math.Abs(
                        Math.IEEERemainder(hashAsString.GetHashCode(), max)
                    )
                );
            }
        }
    }
}
Rollback to Revision 2
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158
Loading
added 157 characters in body
Source Link
Wagacca
  • 179
  • 1
  • 1
  • 7
Loading
edited title
Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading
Source Link
Wagacca
  • 179
  • 1
  • 1
  • 7
Loading