Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Generators/NameGenerator.cs @ 14392

Last change on this file since 14392 was 14392, checked in by pkimmesw, 8 years ago

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

File size: 1.3 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Random;
4
5namespace HeuristicLab.Algorithms.PushGP.Generators
6{
7    public class NameGenerator
8    {
9        public const int DefaultRandomNameLength = 10;
10        private static IRandom rand = new FastRandom();
11        private static char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
12
13        private int index = 0;
14
15        public string GetNextString()
16        {
17            var length = index < chars.Length ? 1 : (int)(Math.Log(index, chars.Length)) + 1;
18            var result = new char[length];
19            var tmp = index;
20            index++;
21
22            for (var i = length; i > 0; i--)
23            {
24                var a = (int)Math.Pow(chars.Length, i - 1);
25                var b = tmp / a;
26
27                result[length - i] = chars[b];
28
29                tmp -= a * b;
30            }
31
32            return new string(result);
33        }
34
35        public static string RandomName(int length = DefaultRandomNameLength)
36        {
37            var name = new char[length];
38
39            for (var i = 0; i < length; i++)
40            {
41                name[i] = chars[rand.Next(chars.Length)];
42            }
43
44            return new string(name);
45        }
46    }
47}
Note: See TracBrowser for help on using the repository browser.