Last change
on this file since 14513 was
14513,
checked in by pkimmesw, 7 years ago
|
#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation
|
File size:
1.2 KB
|
Line | |
---|
1 | namespace HeuristicLab.Algorithms.PushGP.Generators {
|
---|
2 | using System;
|
---|
3 | using System.Threading;
|
---|
4 |
|
---|
5 | using HeuristicLab.Algorithms.PushGP.Data.Random;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 |
|
---|
8 | public class NameGenerator {
|
---|
9 | public const int DefaultRandomNameLength = 10;
|
---|
10 |
|
---|
11 | private static readonly ThreadLocal<IRandom> rand = RandomFactory.GetRandom();
|
---|
12 |
|
---|
13 | private static readonly char[] chars =
|
---|
14 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
|
---|
15 |
|
---|
16 | private int index;
|
---|
17 |
|
---|
18 | public string GetNextString() {
|
---|
19 | var length = this.index < chars.Length ? 1 : (int)Math.Log(this.index, chars.Length) + 1;
|
---|
20 | var result = new char[length];
|
---|
21 | var tmp = this.index;
|
---|
22 | this.index++;
|
---|
23 |
|
---|
24 | for (var i = length; i > 0; i--) {
|
---|
25 | var a = (int)Math.Pow(chars.Length, i - 1);
|
---|
26 | var b = tmp / a;
|
---|
27 |
|
---|
28 | result[length - i] = chars[b];
|
---|
29 |
|
---|
30 | tmp -= a * b;
|
---|
31 | }
|
---|
32 |
|
---|
33 | return new string(result);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static string RandomName(int length = DefaultRandomNameLength) {
|
---|
37 | var name = new char[length];
|
---|
38 |
|
---|
39 | for (var i = 0; i < length; i++) name[i] = chars[rand.Value.Next(chars.Length)];
|
---|
40 |
|
---|
41 | return new string(name);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.