如何将随机成员分配到给定的组编号? [英] How do I assign random members into a given group number?
问题描述
我刚开始学习JavaScript而且我是新手。我有这个HTML。这样做,首先选择将创建多少组然后在第一个textarea上输入名称,然后在我单击随机按钮后,它将自动将您在textarea上输入的名称分发到将要创建的组中。名称由文本拆分器分隔,文本拆分器是逗号。请帮助大家:(谢谢大家。
这里是代码
I just started learning JavaScript and I'm new to it. I have this HTML. What this do is, first select how many groups that would be created then input names on the first textarea, then after i click the random button it will automatically distribute the names that you input on the textarea into the groups that would be created. The names are separated by a text splitter which is a comma. Please help guys :( thanks a bunch.
HERE IS THE CODE
<!DOCTYPE html>
<meta charset="UTC-8">
<html>
<script>
//javascript goes here
</script>
<head>
</head>
<body>
<h2>Add a Group</h2>
<hr>
<table>
<td>
<form id="grp">
Groups:
<br>
<input type="number" name="grpmembers" min="1" max="10" id="grps">
</form>
</td>
</table>
<textarea id="inputNames" rows="10" cols="40"></textarea>
<br>
<input type="button" name="Random" value="Random" onclick="textSplitter()">
<br>
<textarea disabled id="results" rows="10" cols="40"></textarea>
<br>
<button>Finish</button>
</body>
</html>
推荐答案
让我们来看看。这是一种在min
和max
之间的间隔中获取随机值的方法:
Let's see. This is a method of getting a random value in the interval betweenmin
andmax
:
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
数字将具有统计上均匀分布的上述间隔。
现在,如果你有一个一组对象,代表一组,你可以把它们放在一个数组 var objects = [object1,object2,...]
;它们是什么并不重要。您可以使用数组元素语法 [someIndex]
选择其中一个对象。如果为 someIndex
生成随机值,您将从该集合中随机选择一个对象。使用这个想法,你可以解决你的问题。
The numbers will have statistically uniform distribution withing the said interval.
Now, if you have a set of some objects, representing, say, a group, you can put them on an array var objects = [object1, object2, ...]
; it does not matter what they are. You can choose one of these objects using the array element syntax, [someIndex]
. If your generate a random value for someIndex
, you will get an object randomly chosen from this set. Using this idea, you can solve your problem.
这篇关于如何将随机成员分配到给定的组编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!