関数random_divide_groups_givenNumbers マニュアル

(The documentation of function random_divide_groups_givenNumbers)

Last Update: 2025/8/7


◆機能・用途(Purpose)

データセットをいくつかのグループにランダムに分割する。 各グループに入れるデータ数を指定するバージョン。
Divide a data set into several groups randomly, by specifying the number of data to be assigned to each group.


◆形式(Format)

#include <random.h>
inline int ∗random_divide_groups_givenNumbers
(const int Ndata_all,const int Ngroups,const int ∗Ndata_in_group)


◆引数(Arguments)

Ndata_all 全データ数。
The total number of data.
Ngroups 分割するグループ数。
The number of groups to create.
Ndata_in_groups 各グループに入れるデータ数を並べた配列。 Ngroups個の配列要素から成り、 その和がNdata_allと一致しなければならない。
An array composed of the number of data to be assigned to each group. The array must have Ngroups components, the sum of which must be equal to Ndata_all.


◆戻り値(Return value)

分割結果。 各データのグループ番号を並べた配列として返す。 すなわち、\(n\)番目のデータが\(m\)番目のグループに入った場合は 戻り値の配列の第\(n\)要素の値が\(m\)となる。 データ番号、グループ番号はいずれも0から始まるものとする。
Result of the division, given as an array composed of the group ID of each data; if \(n\)th data was assigned to \(m\)th group, the \(n\)th array component of the return value has the value \(m\). Here, both the data and group indices start with zero.


◆使用例(Example)

int Ndata_all=10;
int Ngroups=3;
int Ndata_in_group[]={2,3,5};
int ∗groups=random_divide_groups_givenNumbers (Ndata_all,Ngroups,Ndata_in_group);

この例では0-9の整数をグループ0に2個、グループ1に3個、グループ2に5個 となるようにランダムに分割する。 戻り値(groups)は10個の要素から成る配列となり、 その\(n\)番目の配列要素groups[n]には整数nのグループ番号が入る。
In this example, integers from 0 to 9 are divided randomly into group 0 with two members, group1 with three members, and group 2 with five members. The return value (groups) becomes an array of 10 components, whose \(n\)th component (groups[n]) represents the group ID for the integer \(n\).


◆検証(Validation)

上記の例に出力を加えたコードを繰り返し動かしてみて、 毎回結果が変わること(すなわち乱数が有効に働いていること)、 いずれの場合もグループ0,1,2にそれぞれ2要素、3要素、5要素が割り当てられること を確認した。
A program composed of the example above and an output code was examined repeatedly. The results changed run by run, indicating that the random values are in effect, and in all cases 2, 3, and 5 components are assigned to groups 0, 1, and 2, respectively.