[英]Unity random characters in string
How do I repeat generating a random char?如何重复生成随机字符? I want to make a glitched text effect so I set:我想制作一个有故障的文本效果,所以我设置:
textbox.text = st[Random.Range(0, st.Length)];
in my update method.在我的更新方法中。 I do however only get one character out of this line - how would I repeat this process in a string with the length of 5?然而,我只从这一行中得到一个字符 - 我将如何在长度为 5 的字符串中重复这个过程? What I am trying to achieve is to randomly generate 5 characters over and over again.我想要实现的是一遍又一遍地随机生成 5 个字符。 There must be a better way than this:一定有比这更好的方法:
randomChar + randomChar + randomChar + randomChar + randomChar
Thank you in advance!先感谢您!
Could use something similar:可以使用类似的东西:
public static readonly char[] CHARS = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
static void Main(string[] args)
{
static string GenerateGlitchedString(uint wordCount,uint wordslength)
{
string glitchedString = "";
for (int i = 0; i < wordCount; i++)
{
for (int j = 0; j < wordslength; j++)
{
Random rnd = new Random();
glitchedString += CHARS[rnd.Next(CHARS.Length)];
}
glitchedString += " "; //Add spaces
}
return glitchedString;
}
string output = GenerateGlitchedString(5, 5);
Console.WriteLine(output);
}
You can use Linq to generate any number of random characters from an enumerable:您可以使用 Linq 从可枚举中生成任意数量的随机字符:
int howManyChars = 5;
var newString = String.Join("", Enumerable.Range(0, howManyChars).Select(k => st[Random.Range(0, st.Length)]));
textbox.text = newString;
If you want completely glitchy strings, go the way of Mojibake .如果你想要完全有问题的字符串,go Mojibake的方式。 Take a string, convert it to one encoding and then decode it differently.取一个字符串,将其转换为一种编码,然后以不同的方式对其进行解码。 Most programmers end up familiar with this kind of glitch eventually.大多数程序员最终都会熟悉这种故障。 For example this code:例如这段代码:
const string startWith = "Now is the time for all good men to come to the aid of the party";
var asBytes = Encoding.UTF8.GetBytes(startWith);
var glitched = Encoding.Unicode.GetString(asBytes);
Debug.WriteLine(glitched);
consistently results in:始终导致:
潎⁷獩琠敨琠浩潦污潧摯洠湥琠潣敭琠桴楡景琠敨瀠牡祴
But, if you just want some text with some random glitches, how about something like this.但是,如果你只是想要一些带有一些随机故障的文本,那么这样的东西怎么样。 It uses a set of characters that should be glitched in, a count of how many glitches there should be in a string (based on the string length) and then randomly inserts glitches:它使用一组应该出现故障的字符,计算一个字符串中应该有多少个故障(基于字符串长度),然后随机插入故障:
private static Random _rand = new Random();
private const string GlitchChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%&";
public static string GlitchAString(string s)
{
var glitchCount = s.Length / 5;
var buffer = s.ToCharArray();
for (var i = 0; i < glitchCount; ++i)
{
var position = _rand.Next(s.Length);
buffer[position] = GlitchChars[_rand.Next(GlitchChars.Length)];
}
var result = new string(buffer);
Debug.WriteLine(result);
return result;
}
The first two times I ran this (with that same Now is the time...
string), I got:前两次我运行这个(同样Now is the time...
字符串),我得到:
Original String:
Now is the time for all good men to come to the aid of the party
First Two Results:
LowmiW HhZ7time for all good mea to comX to ths aid oV the p1ray
Now is fhO P!me forjall gKod men to @ome to the a@d of F5e Nawty
The algorithm is, to some extent, tunable.该算法在某种程度上是可调的。 You can have more or fewer glitches.您可以有更多或更少的故障。 You can change the set of glitch chars - whatever you'd like.您可以更改故障字符集 - 随心所欲。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.