void CStars::initStars() { float fy, fp; CVector3 vecPos; int i; m_iNumStars = 500; for (i = 0; i < 500; i++) { fy = random01() * 360; fp = random01() * 360; CQuat quaTemp(DEG_TO_RAD(fy), 0.0f, DEG_TO_RAD(fp)); CMatrix mat(quaTemp); vecPos = CVector3(0.0f, 0.0f, -1.0f); m_aoStars[i].m_vecPos = mat* vecPos; m_aoStars[i].m_vecPos *= 9000.0f; m_aoStars[i].m_iTexture = 0; fy = random01(); if (fy < 0.33f) { m_aoStars[i].m_fSize = 15.0f; } else { if (fy < 0.66f) { m_aoStars[i].m_fSize = 25.0f; } else { m_aoStars[i].m_fSize = 35.0f; } } //m_aoStars[i].m_vecColor.X() = RANDOM_FLOAT; //m_aoStars[i].m_vecColor.Y() = RANDOM_FLOAT; //m_aoStars[i].m_vecColor.Z() = RANDOM_FLOAT; } // Create sprite CMaterial oMaterial; oMaterial.m_oEmissive = CRGBAColour(1.0f, 1.0f, 1.0f,0); oMaterial.m_uiTexture = g_oTextureManager.load("an01.png"); m_oSprite = CSprite(oMaterial); m_oSprite.init(); //m_auiTextures[1] = g_oTextureManager.load("an02.png"); //m_auiTextures[2] = g_oTextureManager.load("an03.png"); }
bool catchProbability(float p) //the probability of return true is p { float r=random01(); if(r<p)return true; else return false; }
std::complex<double> Random::randomGauss2d() { double r = randomGauss(); double a = 2. * M_PI * random01(); return std::complex<double>(r * cos(a), r * sin(a)); }
void SceneRope::init() { if (m_system) delete m_system; m_system = new ParticleSystem(); m_gravityForce = new ConstantForce(); m_gravityForce->setGravityConstant(Vec3d(0.0, -DataManager::mGravityValue, 0.0)); m_system->addForce(m_gravityForce); m_floor = new CollisionPlane(); m_floor->setPlane(Vec3d(0,1,0), 0); m_ball = new CollisionSphere(); m_ball->useInnerSide(false); m_ball->setPosition(DataManager::mSpringBall); m_ball->setRadius(DataManager::mSpringBallRadius); std::vector<Particle> vparts; double eps = DataManager::mCollisionEpsilon; for (int i = 0; i < DataManager::mRopeParticles; i++) { Vec3d pp(double(i)/10.0, 3, 0); Vec3d pv(0, 0, 0); Particle* p = new Particle(pp, pv, 1.0); p->color = Vec3f(random01(), random01(), random01()); m_system->addParticle(p); vparts.push_back(*p); if (i > 0) { m_gravityForce->addInfluencedParticle(p); Particle* p2 = m_system->getParticle(i-1); MassSpringForce* msf = new MassSpringForce(); msf->setParticlePair(p2, p); msf->setRestingLength(len(p2->pos - p->pos)); msf->setSpringCoefficient(DataManager::mSpringK); msf->setDampingCoefficient(DataManager::mSpringDamp); m_system->addForce(msf); } else { p->mass = 1e32; } } DataManager::mParticles.push_back(vparts); }
void CtinyWingsTerrainSprite::reGenerateShape(int insertControlPointCount){ m_insertControlPointCount=insertControlPointCount; vector<CCPoint>&pointList=m_pointMat[0]; //generate controlPoints CCPoint startPoint=pointList[0]; CCPoint endPoint=pointList[(int)pointList.size()-1]; float len=endPoint.x-startPoint.x; float stepLen=len/(insertControlPointCount+1); vector<CCPoint> controlPointList; controlPointList.push_back(CCPoint(startPoint.x,m_height));//start control point for(int i=0;i<insertControlPointCount;i++){ float x=(i+1)*stepLen; float y=m_height; controlPointList.push_back(CCPoint(x,y)); } controlPointList.push_back(CCPoint(endPoint.x,m_height));//end control point. now got controlPointList //random y for each control point int nControlPoint=(int)controlPointList.size(); for(int i=0;i<nControlPoint;i++){ CCPoint&controlPoint=controlPointList[i]; float dy=(random01()*2-1)*MIN(m_height*0.8,stepLen*0.7); if(i>0&&dy*(controlPointList[i-1].y-m_height)>0){ dy=-dy; } controlPoint.y=m_height+dy; } //reshape pointList int controlPointIndex=0; int nPoint=(int)pointList.size(); for(int i=0;i<nPoint;i++){ CCPoint&point=pointList[i]; //find the first control point whose x is bigger than point.x while(controlPointIndex<(int)controlPointList.size()&&controlPointList[controlPointIndex].x<=point.x){ controlPointIndex++; } if(controlPointIndex>=(int)controlPointList.size()){//point is endPoint point=controlPointList[(int)controlPointList.size()-1]; }else{ assert(controlPointIndex>=1); CCPoint controlPoint=controlPointList[controlPointIndex-1]; CCPoint controlPointn=controlPointList[controlPointIndex]; //get point.y by sin interpolation const float dx=controlPointn.x-controlPoint.x; const float dy=controlPointn.y-controlPoint.y; point.y=controlPoint.y+dy/2*(1-cosf((M_PI/dx)*(point.x-controlPoint.x))); } int nRow=(int)m_pointMat.size(); for(int j=1;j<nRow;j++){ CCPoint&_point=m_pointMat[j][i]; _point.y=point.y*(1-pow((float)j/(nRow-1),1));//must convert j to float !!! } } //update mesh and submit updateMesh(); submit(); }
float randgen(float fLlim, float fUlim) { float fRandomVal; fRandomVal = random01(); return( fLlim + (float) (fRandomVal * (fUlim - fLlim)) ); }
void testIntegration(){ long int r; r=1867; printf("\nIntergration\n"); Integration(funcTest,simpsonsRule,0.0,1.0); Integration(funcTest,trapizoidalRule,0.0,1.0); printf("monteCarloIntegration1D: %f\n",monteCarloIntegration1D(funcTest,0.0,1.0,1e7)); printf("simpsonsRule: %f\n",simpsonsRule(funcTest,0.0,1.0,100)); printf("random01:%f\n",random01()); printf("random01Self: %f\n",random01Self(&r)); printf("trapizoidalRule2D: %f\n",trapizoidalRule2D(funcTest5, 0, 1, 0, 1, 10,10)); printf("trapizoidalRule2D: %f\n",trapizoidalRule2D(funcTest6, 1, 2, 3, 5, 40,40)); }
void Random::init() { double * iEnd = gauss_ + gaussS_; for (double * i = gauss_; i != iEnd; ++i) *i = randomGauss(); std::complex<double> * jEnd = gauss2d_ + gaussS_; for (std::complex<double> * j = gauss2d_; j != jEnd; ++j) *j = randomGauss2d(); iEnd = uniform01_ + uniformS_; for (double * i = uniform01_; i != iEnd; ++i) *i = random01(); iEnd = uniform11_ + uniformS_; for (double * i = uniform11_; i != iEnd; ++i) *i = random11(); }
// ============================================================================ /// Splits polygon in two QList<QPolygonF> splitPolygonRandomly( const QPolygonF& polygon, QLineF* pdebugout ) { QRectF br = polygon.boundingRect(); // get random angle on cutting double angle = random01()* 6.28; // cut throught the center QPointF center = br.center(); double upper = br.width() + br.height(); // upper bound for any of the polygon's dimensions QPointF p1( center.x() - upper*sin(angle), center.y() - upper*cos(angle) ); QPointF p2( center.x() + upper*sin(angle), center.y() + upper*cos(angle) ); // debug if ( pdebugout ) *pdebugout = QLineF( p1, p2 ); return splitPolygon( polygon, QLineF( p1, p2 ) ); }
/* ALLOCATE TREATMENT USING MINIMIZATION */ void allot_minimization( ALLOT_STRATEGY *tm, int *current_val ) { int i; int first = 0, last = 1; for ( i = 0; i < tm->ntreat; i++ ) /* FIND LOWEST COUNT */ {tm->choice[i].index = i; tm->choice[i].metric = allot_minimization_count( tm, tm->past[i], current_val ); tm->choice[i].rmetric = (double) tm->choice[i].metric + 0.1 * random01(); /* RANDOMLY ASSORT TIES */ tm->choice[i].rmetric /= (double) tm->choice[i].weight; } qsort( tm->choice, (size_t) tm->ntreat, sizeof(ALLOT_STRATEGY_CHOICE), sort_cmp_ASCHOICE ); if ( debug_allot > 0 ) {for ( i = 0; i < tm->ntreat; i++ ) {aside( "choice[%d]\tmetric=%d\trmetric=%lg\tindex=%d", i, tm->choice[i].metric, tm->choice[i].rmetric, tm->choice[i].index ); } } tm->okay = YES; }
float randomInRange(float min,float max){ return min+random01()*(max-min); }
inline bool in_probability(double x) { return random01()<=x; }
T operator()(T from,T to)const { return static_cast<T>(from+(to-from)*random01()); }
double Utils::randomRange(double min, double max) const { if (min >= max) return min; return random01() * (max - min) + min; }
namespace_ens_begin void CtinyWingsTerrainSprite::init(float width,float height,int insertControlPointCount){ this->CCSprite::init(); m_width=width; m_height=height; m_pointMat.resize(m_nRow); vector<CCPoint>&pointList=m_pointMat[0]; int nseg=ceilf(m_width/m_dx); m_dx=m_width/nseg;//revise dx int nPoint=nseg+1; for(int i=0;i<nPoint;i++){ float x=i*m_dx; CCPoint point(x,m_height); pointList.push_back(point); int nRow=(int)m_pointMat.size(); for(int j=1;j<nRow;j++){ CCPoint _point; _point.x=point.x; _point.y=point.y*(1-(float)j/(nRow-1));//must convert j to float !!! m_pointMat[j].push_back(_point); } }//got m_pointMat; //----create mesh m_mesh=new Cmesh(); m_mesh->autorelease(); m_mesh->retain(); //----create indexVBO m_indexVBO=new CindexVBO(); m_indexVBO->autorelease(); m_indexVBO->retain(); //----colorList m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); //----reShape reGenerateShape(insertControlPointCount); //----update mesh updateMesh(); //----submitMesh submit(); //----create and set shader program { GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("shaders/tinyWingsTerrain_nonlinearTexCoord.fsh").c_str())->getCString(); CGLProgramWithUnifos* program = new CGLProgramWithUnifos(); program->autorelease(); program->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); //bind attribute program->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); program->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color); program->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); //link (must after bindAttribute) program->link(); //get cocos2d-x build-in uniforms program->updateUniforms(); //get my own uniforms program->attachUniform("u_texNonlinearFactor"); program->attachUniform("u_colors"); program->attachUniform("u_cosA"); program->attachUniform("u_sinA"); program->attachUniform("u_ribbonRepeat"); //set program m_program=program; m_program->retain(); //check gl error CHECK_GL_ERROR_DEBUG(); } }
void CtinyWingsTerrainSprite::reGenerateColors(){ m_colorList.clear(); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); m_colorList.push_back(ccc4f(random01(), random01(), random01(), 1)); }