如何return多个值,或者如何让每个iteration的Random number 为同一个值



  • 下面的代码其实是错误的,但是我想问一下如何能实现 return多值。
    我需要return dClass,PUr和PUz。
    实在是对于编程这块了解的不够深。

    我尝试过分成三个class,但是问题就是分成三个class以后,每次调用的随机数就不一样了。

    拜谢!

    Foam::scalar Foam::distributionModels::PDF::sample() const
    {
        scalar y = rndGen_.sample01<scalar>(); //dummy
    	
    	Info<< "\n number1PDF = "  << y << endl;
    
        return y; //dummy
    }
    Foam::scalar Foam::distributionModels::PDF::samplePDF(const PDFDataIOList& PDFsamples) const
    {
    
        // Generating random
       const scalar y = this->sample();
    	//scalar y = sample();
        // Looking for the diameter class
        int classID = 0;
        int classIDmax = PDFsamples.size() - 1;
        while (y > PDFsamples[classID].prob() && classID < classIDmax) 
    	{
          classID++;
        }
    
     	scalar dClassAvg = PDFsamples[classID].dclass();
    	scalar dClassDevP = PDFsamples[classID].devPclass();
    	scalar dRandon = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar dClass = dClassAvg + dClassDevP*dRandon;
    	scalar	dmass	=	1.0/6.0*3.141592653*dClass*dClass*dClass*1000;
    
    	
    	scalar PUrAvg = PDFsamples[classID].Ur();
    	scalar PUrrms = PDFsamples[classID].Urrms();
    	scalar PRandonur = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar PUr = PUrAvg + PUrrms * PRandonur;
    
    	scalar PUzAvg = PDFsamples[classID].Uz();
    	scalar PUzrms = PDFsamples[classID].Uzrms();
    	scalar PRandonuz = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar PUz	= PUzAvg+PUzrms*PRandonuz;
    
    	Info<< "\n -------------------------------------------------------------- samplePDF";
    	Info<< "\n random number1 = "  << y << endl;
    	Info<< "\n classID1 = "  << classID << endl;
    	Info<< "\n classIDmax1 = "  << classIDmax << endl;
    
    	Info<< "\n dClassAvg1 = "  << dClassAvg << "		dClassDevP = "  << dClassDevP << endl;
    	Info<< "\n dRandon1 = "  << dRandon << "		dClass = "  << dClass << "		dmass = "<< dmass << endl;
    	Info<< "\n -------------------------------------------------------------- samplePDF";
    		
    		Info<< "\n -------------------------------------------------------------- samplePDFUr";
    	Info<< "\n random number = "  << y << endl;
    	Info<< "\n classID = "  << classID << endl;
    	Info<< "\n PUrAvg = "  << PUrAvg << "		PUrrms = "  << PUrrms << endl;
    	Info<< "\n PRandonur = "  << PRandonur << "		PUr = "  << PUr << endl;	
    	Info<< "\n -------------------------------------------------------------- samplePDFUr";
    
    
    
    	Info<< "\n -------------------------------------------------------------- samplePDFUz";
    	Info<< "\n random number2 = "  << y << endl;
    	Info<< "\n classID2 = "  << classID << endl;
    
    	Info<< "\n PUzAvg2 = "  << PUzAvg << "		PUzrms = "  << PUzrms << endl;
    	Info<< "\n PRandonuz2 = "  << PRandonuz << "		PUz = "  << PUz << endl;
    	Info<< "\n -------------------------------------------------------------- samplePDFUz";
    
    	return PUr;
    	return PUz
        return dClass;
    
    }
    
    Foam::scalar Foam::distributionModels::PDF::sample() const
    {
        scalar y = rndGen_.sample01<scalar>(); //dummy
    	
    	Info<< "\n number1PDF = "  << y << endl;
    
        return y; //dummy
    }
    
    Foam::scalar Foam::distributionModels::PDF::samplePDF(const PDFDataIOList& PDFsamples) const
    {
    
        // Generating random
       const scalar y = this->sample();
    	//scalar y = sample();
        // Looking for the diameter class
        int classID = 0;
        int classIDmax = PDFsamples.size() - 1;
        while (y > PDFsamples[classID].prob() && classID < classIDmax) 
    	{
          classID++;
        }
    
     	scalar dClassAvg = PDFsamples[classID].dclass();
    	scalar dClassDevP = PDFsamples[classID].devPclass();
    	scalar dRandon = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar dClass = dClassAvg + dClassDevP*dRandon;
    	scalar	dmass	=	1.0/6.0*3.141592653*dClass*dClass*dClass*1000;
    
    	
    	
    
    	Info<< "\n -------------------------------------------------------------- samplePDF";
    	Info<< "\n random number1 = "  << y << endl;
    	Info<< "\n classID1 = "  << classID << endl;
    	Info<< "\n classIDmax1 = "  << classIDmax << endl;
    
    	Info<< "\n dClassAvg1 = "  << dClassAvg << "		dClassDevP = "  << dClassDevP << endl;
    	Info<< "\n dRandon1 = "  << dRandon << "		dClass = "  << dClass << "		dmass = "<< dmass << endl;
    	Info<< "\n -------------------------------------------------------------- samplePDF";
    	
        // Returning the class diameter
    	
        return dClass;
    
    }
    
    Foam::scalar Foam::distributionModels::PDF::samplePDFUz(const PDFDataIOList& PDFsamples) const
    {
        // Generating random
       // scalar y = rndGen_.sample01<scalar>();
    	const scalar y = this->sample();
        
    	// Looking for the diameter class
        int classID = 0;
        int classIDmax = PDFsamples.size() - 1;
        while (y > PDFsamples[classID].prob() && classID < classIDmax) 
    	{
          classID++;
        }
    	scalar PUzAvg = PDFsamples[classID].Uz();
    	scalar PUzrms = PDFsamples[classID].Uzrms();
    	scalar PRandonuz = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar PUz	= PUzAvg+PUzrms*PRandonuz;
    
    	Info<< "\n -------------------------------------------------------------- samplePDFUz";
    	Info<< "\n random number2 = "  << y << endl;
    	Info<< "\n classID2 = "  << classID << endl;
    
    	Info<< "\n PUzAvg2 = "  << PUzAvg << "		PUzrms = "  << PUzrms << endl;
    	Info<< "\n PRandonuz2 = "  << PRandonuz << "		PUz = "  << PUz << endl;
    	Info<< "\n -------------------------------------------------------------- samplePDFUz";
    
    	return PUz;
    }
    
    Foam::scalar Foam::distributionModels::PDF::samplePDFUr(const PDFDataIOList& PDFsamples) const
    {
        // Generating random
        //scalar y = rndGen_.sample01<scalar>();
    	const scalar y = this->sample();
        // Looking for the diameter class
        int classID = 0;
        int classIDmax = PDFsamples.size() - 1;
        while (y > PDFsamples[classID].prob() && classID < classIDmax) 
    	{
          classID++;
        }
    		
    	
    	scalar PUrAvg = PDFsamples[classID].Ur();
    	scalar PUrrms = PDFsamples[classID].Urrms();
    	scalar PRandonur = (2.0 - ROOTVSMALL)*rndGen_.sample01<scalar>() - 1.0;
    	scalar PUr = PUrAvg + PUrrms * PRandonur;
    		Info<< "\n -------------------------------------------------------------- samplePDFUr";
    	Info<< "\n random number = "  << y << endl;
    	Info<< "\n classID = "  << classID << endl;
    	Info<< "\n PUrAvg = "  << PUrAvg << "		PUrrms = "  << PUrrms << endl;
    	Info<< "\n PRandonur = "  << PRandonur << "		PUr = "  << PUr << endl;	
    	Info<< "\n -------------------------------------------------------------- samplePDFUr";
    	return PUr;
    }
    


  • 调用的时候是再injection model中 调用的,目前还是测试。。
    只要能得到正确的值就可以使用了。。。

    分成3个class以后,每一次赋值,都需要进入sample()中重新获取随机数。

    parcel.d() = sizeDistribution_->samplePDF(PDFsamples_);	
    
    scalar uztest = sizeDistribution_->samplePDFUz(PDFsamples_);
    	Info<< "\n---------------------in injectionmodel  "  << endl;
    	Info<< "\n uztest = "  << uztest << endl;
    
    scalar urtest = sizeDistribution_->samplePDFUr(PDFsamples_);
    	Info<< "\n---------------------in injectionmodel  "  << endl;
    	Info<< "\n urtest = "  << urtest << endl;
    


  • @星星星星晴

    :wolaile: 没太细看哈不过

    我需要return dClass,PUr和PUz。

    你可以把这三个打包成一个struct或者class,而不是三个哦,然后返回这个struct或者class,差不多这种

    class test
    {
    public:
        scalar A_;
        scalar B_;
    }
    
    test returnFunction(test& f)
    {
        f.A_ = 1;
        f.B_ = 2;
        return f;
    }
    

Log in to reply