Visual c++ 生成随机1';s和0';s

Visual c++ 生成随机1';s和0';s,visual-c++,random,generator,Visual C++,Random,Generator,下面的代码随机产生相等数量的1和0,这是它假定要做的 然而,我想知道如何获得1岁的%年龄和0岁的剩余年龄。我需要在代码中进行哪些更改。期待您的回复 int random_gen::uniformI(int low, int high) { int i; if (low >= high) i = low; else { i = (int) (randomperc() * (high - low + 1)) + low;

下面的代码随机产生相等数量的1和0,这是它假定要做的

然而,我想知道如何获得1岁的%年龄和0岁的剩余年龄。我需要在代码中进行哪些更改。期待您的回复

int random_gen::uniformI(int low, int high)
{
    int i;

    if (low >= high)
        i = low;
    else {
        i = (int) (randomperc() * (high - low + 1)) + low;
        if(i > high) 
            i = high;
    }
    return(i);
}

//main
for (int l=0; l<NUM_NODES;l++) { 
    wkey = nodes[i].WitnessKey;
}
cout<<wkey
int-random\u-gen::uniformI(int-low,int-high)
{
int i;
如果(低>=高)
i=低;
否则{
i=(int)(randomperc()*(高-低+1))+低;
如果(i>高)
i=高;
}
回报(i);
}
//主要
对于(int l=0;l在以给定的1s百分比初始化的数组上使用。以下实现了25%:

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <iterator>

void shuffle(double percentage,std::vector<int> &l) {
    if (l.empty()) return;

    std::mt19937 gen;
    unsigned int N_Ones = static_cast<unsigned int>(floor(percentage*l.size()));

    l[0] = (N_Ones > 0)?1:0;
    for(unsigned int i = 1; i < l.size() - 1; ++i) {
        std::uniform_int_distribution<unsigned int> ui(0,i);
        unsigned int j = ui(gen);
        l[i] = l[j];
        l[j] = (N_Ones > 0)?1:0;
    }
}

int main(int argc, char* argv[])
{
    unsigned int N = 10;
    if (argc >= 1) N = atoi(argv[1]);
    std::vector<int> l(N,0);
    shuffle(0.25,l);
    std::copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout,""));
    std::cout << std::endl;
    return 0;
}    
#包括
#包括
#包括
#包括
#包括
无效洗牌(双倍百分比,标准::矢量和l){
if(l.empty())返回;
标准:mt19937 gen;
unsigned int N_Ones=静态(floor(百分比*l.size());
l[0]=(N个>0)?1:0;
for(无符号整数i=1;i0)?1:0;
}
}
int main(int argc,char*argv[])
{
无符号整数N=10;
如果(argc>=1)N=atoi(argv[1]);
std::向量l(N,0);
洗牌(0.25,l);
std::copy(l.begin(),l.end(),std::ostream_迭代器(std::cout,“”);

std::以前无法多次询问类似问题。请尝试搜索。。。