(The documentation of function random_divide_2groups_givenRatio)
Last Update: 2025/8/7
◆機能・用途(Purpose)
データセットを2つのグループにランダムに分割する。
最初のグループに入れるデータの比率を指定するバージョン。
Divide a data set into two groups randomly,
by specifying the ratio of data to be assigned to the earlier group.
◆形式(Format)
#include <random.h>
inline int ∗random_divide_2groups_givenRatio
(const int Ndata_all,const double group0_ratio)
◆引数(Arguments)
Ndata_all
全データ数。
The total number of data.
group0_ratio
グループ0に入れるデータの割合。
The ratio of data to be assigned to group 0.
◆戻り値(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 ∗groups=random_divide_2groups_givenRatio(12,0.3);
この例では0∼11の整数をグループ0:グループ1=0.3:0.7(=3:7)となるようにランダムに分割する。
但しこの比率だとグループ0が3.6個となるが、これを四捨五入してグループ0が4個になるように分割する。
戻り値(groups)は12個の要素から成る配列となり、
その\(n\)番目の配列要素groups[n]には整数nのグループ番号が入る。
In this example, integers from 0 to 11 are divided randomly into
groups 0 and 1 with a ratio 0.3:0.7 (= 3:7).
Exactly, this ratio means that the group 0 should have 3.6 components;
indeed, by rounding this number, the group 0 will have 4 components.
The return value (groups) becomes
an array of 12 components,
whose \(n\)th component (groups[n]) represents
the group ID for the integer \(n\).
◆検証(Validation)
上記の例に出力を加えたコードを繰り返し動かしてみて、
毎回結果が変わること(すなわち乱数が有効に働いていること)、
いずれの場合もグループ0,1にそれぞれ4要素、8要素が割り当てられること
を確認した。
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 4 and 8 components are assigned to
groups 0 and 1, respectively.