我想通过插入-
字符来欺骗字符串。我上课了:
static class Spoofer {
private static char spoofChar = '-';
private static Random rnd = new Random();
public static double SpoofPercentage = 0.3;
public static string Spoof(string input) {
if (input.Length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++) {
while (rnd.NextDouble() <= SpoofPercentage) {
sb.Append(spoofChar);
}
sb.Append(input[i]);
}
return sb.ToString();
}
}
这就是我使用欺骗的方式:
string messageToSpoof = "This is my example message";
string spoofedMessage = Spoofer.Spoof(messageToSpoof);
这就是欺骗性消息的样子:
-Th-is is my- e--x-am--p--l-e messa-g-e
--T-his is --my --exampl--e me--s-sage
This --is my exam--p-le mes-sage
This- -i-s m-y-- example-- ---mess---a---ge
Th-is is my example -message
Th-i----s is m-y- exam-p-le -message
This is-- -my examp--l--e me-ssa-g-e
This -is --my- e-xa--mple m----ess-age
T-his -is--- my- exa-m-ple me---s-s-ag-e
因此,您可以看到-
并未真正随机插入。很容易观察到一种模式。在大多数情况下-
放置几次。我怎么能避免这种情况?我认为问题是,Random
对象产生了类似的值。
答案 0 :(得分:0)
他们看起来很随意。
听起来你正面临苹果曾经用它的歌曲洗牌功能所面临的同样问题。当人们看到随机的信息集群时,我们自然地叠加了一种模式(不一定存在)。这就是我们将秩序状态投射到混乱中。
如果你没有重复-
的字符串。在Spoofer
课程中添加一项检查,if
而不是while
语句。但这本来就不那么随意了。
答案 1 :(得分:0)
随机数生成器(针对其所有错误)返回0到1之间的值。获得破折号的上限为.3,这样您在10中有3次机会获得破折号。在某些情况下,我会看到在10个字符的范围内,你得到的小于3的斑点,以及有超过10个字符的其他斑点。这是随机数生成的预期。 (曾经在桌面游戏中骰子?)
如果你太多了,降低你的天花板。或者,找到一种生成随机数的不同方法。
随机数生成器不会生成真正的随机数。它们最多是伪随机的。