在字符串中更改随机字符
一天中的好时机
我必须在网站上实施一些功能。 当用户进入网站时,他必须输入一些应该在某些字符串中发生的错误。如果他选择0,则将显示原始字符串而不会出错。如果用户在错误字段中输入1,则应以1个错误显示字符串。例如,应该显示“编程”一词,例如“ rrograming”或“ prugramming”,或将一个字符添加到字符串中。因此,如果错误是2,则错误也应为2。
此外,每次都应相同的结果。有人告诉我在随机班上使用种子,但是现在我不知道。
我正在Java和一些JS编程
,如果您面临同样的问题或经验,请给我一些想法或资源来学习。
Good time of the day
I have to implement some functions on my site.
When a user enters the website, he has to enter a number of errors that should be occurred in some string. If he chooses 0, the original string will be displayed without error. If the user enters 1 in the error field, the string should be displayed with 1 error. For instance, the word "programming" should be displayed like "rrograming" or "prugramming", or adding/deleting one character to/from the string. Consequently, if the error is 2, the mistakes also should be 2.
In addition, the result should be the same every time. Someone told me to use seeds in Random class, but now I don't have idea.
I am programming in Java and some JS
Please, if you faced the same problem or experience, give me some ideas or resources to learn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
随机带有种子每次都会产生相同的值序列,因此每次使用相同的随机性。
诀窍是根据单词内容使用随机种子。否则,您最终将对所有单词都采用相同的肢解模式。
更改字母的可能性可以按照单词的长度顺序。
并插入两倍的删除,两倍很少见。
由于Java 12可以使用上述开关表达式。
以上也会肢解肢解,因此您可能更喜欢首先具有a times 循环更改随机字母(或不),然后是第二个 times loop添加/添加/删除字母。
Random with a seed results in the same sequence of values every time, so use the same random every time.
The trick is using a random seed based on the word content. Otherwise you would end up doing the same mutilation pattern on all words.
The likeliness of changing a letter can be in the order of the length of the word.
And inserting twice as much as removing, both rare.
Since java 12 one can use a switch expression as above.
The above will also mutilate the mutilations, so you might prefer to first have a times loop changing random letters (or not), and then a second times loop adding/removing letters.
您朋友的想法是正确的,每次将随机数种子设置为相同的值,将产生相同的结果。但是,例如,您每次在地图中计算结果时都可以缓存结果。将密钥设置为误差编号,值将键作为字符串结果。在每个函数调用中,您检查错误号是否被缓存,如果是这样,则您将其返回而不计算它,请计算结果,然后将其缓存并最终将其返回。
Your friend's idea is correct, setting the random number seed to the same value each time will yield same results. However, you can cache the result everytime you compute it in a map for example. Setting the keys as the error number and the value as the string result. On each function call you check if the error number is cached, if that's the case you return it without computing it else you compute the result then cache it and finally return it.