示例#1
0
//------ Begin of function Math::get_random_snd -------//
//!
//! returns a random value in standard normal distribution
//! with average avg and standard deviation sd
//! write code here according to the book "Numerical Receipe in C"
//!
//! nonNegative:	trancate the return value to zero or positive value;
//!						default=false;
//!
float Math::get_random_snd(float avg, float sd, bool returnNonNegative) {
    // 1. calc ret which is a normally distributed deviate with zero mean and unit variance
    static bool iset=false;
    static float gset;

    float fac, rsq, ret, v1, v2;

    if ( !iset ) {
	do {
	    v1 = 2.0f * get_random_float() - 1.0f;
	    v2 = 2.0f * get_random_float() - 1.0f;
	    rsq = v1*v1 + v2*v2;
	}
	while (rsq >= 1.0 || rsq == 0.0);

	fac = (float) sqrt(-2.0f * log(rsq) / rsq );
	iset = true;
	gset = v1 * fac;
	ret = v2 * fac;
    }
    else {
	iset = false;
	ret = gset;
    }

    // 2. re-scale gset for return
    if ( returnNonNegative )
	return max(0.0f, gset * sd + avg);            // min & max bug chea
    else
	return gset*sd + avg;
}
		color_vector Emitter::getColor() const
		{
			if(color_range_) {
				return glm::detail::tvec4<unsigned char>(
					get_random_float(color_range_->first.r,color_range_->second.r),
					get_random_float(color_range_->first.g,color_range_->second.g),
					get_random_float(color_range_->first.b,color_range_->second.b),
					get_random_float(color_range_->first.a,color_range_->second.a));
			}
			color_vector c;
			c.r = uint8_t(color_.r * 255.0f);
			c.g = uint8_t(color_.g * 255.0f);
			c.b = uint8_t(color_.b * 255.0f);
			c.a = uint8_t(color_.a * 255.0f);
			return c;
		}
SharedPortEndpoint::SharedPortEndpoint(char const *sock_name):
	m_is_file_socket(true),
	m_listening(false),
	m_registered_listener(false),
	m_retry_remote_addr_timer(-1),
	m_max_accepts(8),
	m_socket_check_timer(-1)
{

		// Now choose a name for this listener.  The name must be unique
		// among all instances of SharedPortEndpoint using the same
		// DAEMON_SOCKET_DIR.  We currently do not check for existing
		// sockets of the same name.  Instead we choose a name that
		// should be unique and later blow away any socket with that
		// name, on the assumption that it is junk left behind by
		// somebody.  Since our pid is in the name, this is a reasonable
		// thing to do.

	if( sock_name ) {
			// we were given a name, so just use that
		m_local_id = sock_name;
	}
	else {
		static unsigned short rand_tag = 0;
		static unsigned int sequence = 0;
		if( !rand_tag ) {
				// We use a random tag in our name so that if we have
				// re-used the PID of a daemon that recently ran and
				// somebody tries to connect to that daemon, they are
				// unlikely to connect to us.
			rand_tag = (unsigned short)(get_random_float()*(((float)0xFFFF)+1));
		}

		if( !sequence ) {
			m_local_id.formatstr("%lu_%04hx",(unsigned long)getpid(),rand_tag);
		}
		else {
			m_local_id.formatstr("%lu_%04hx_%u",(unsigned long)getpid(),rand_tag,sequence);
		}

		sequence++;
	}
#ifdef WIN32
	wake_select_source = NULL;
	wake_select_dest = NULL;

	kill_thread = false;
	thread_killed = INVALID_HANDLE_VALUE;

	pipe_end = INVALID_HANDLE_VALUE;
	inheritable_to_child = INVALID_HANDLE_VALUE;

	thread_handle = INVALID_HANDLE_VALUE;

	InitializeCriticalSection(&received_lock);
	InitializeCriticalSection(&kill_lock);
#endif
}
		float Emitter::generateAngle() const
		{
			ASSERT_LOG(technique_ != NULL, "PSYSTEM2: technique_ is null");
			ASSERT_LOG(technique_->getParticleSystem() != NULL, "PSYSTEM2: technique_->get_parent_system() is null");
			float angle = angle_->getValue(technique_->getParticleSystem()->getElapsedTime());
			if(angle_->type() == ParameterType::FIXED) {
				return get_random_float() * angle;
			}
			return angle;
		}
示例#5
0
			explicit emit_object(particle_system_container* parent, const variant& node) 
				: parent_container_(parent) {
				ASSERT_LOG(parent != NULL, "FATAL: PSYSTEM2: parent is null");
				if(node.has_key("name")) {
					name_ = node["name"].as_string();
				} else {
					std::stringstream ss;
					ss << "emit_object_" << int(get_random_float());
					name_ = ss.str();
				}
			}
示例#6
0
文件: ga.cpp 项目: franaln/evolve
int GA::roulette()
{
  double r = get_random_float(0.0, m_total_fitness);

  double accumulate = 0.0;
  for(unsigned int i=0; i<m_population_size; i++){
    accumulate += m_population[i]->get_fitness();

    if(r < accumulate)
      return i;
  }
  return 0;
}
示例#7
0
/*******************************************************************************
* vector_t create_random_vector(int len)
*
* 
*******************************************************************************/
vector_t create_random_vector(int len){
	int i;
	vector_t v = create_empty_vector();
	if(len<1){
		printf("error creating vector, len must be >=1");
		return v;
	}
	v = create_vector(len);
	for(i=0;i<len;i++){
		v.data[i]=get_random_float();
	}
	return v;
}
示例#8
0
/*******************************************************************************
* matrix_t create_random_matrix(int rows, int cols)
*
* 
*******************************************************************************/
matrix_t create_random_matrix(int rows, int cols){
	int i,j;
	matrix_t A;
	if(rows<1 || cols<1){
		printf("error creating matrix, row or col must be >=1");
		return A;
	}
	A = create_matrix(rows, cols);
	for(i=0;i<rows;i++){
		for(j=0;j<cols;j++){
			A.data[i][j]=get_random_float();
		}
	}
	return A;
}
示例#9
0
void get_random_vector(D3DXVECTOR3* out, D3DXVECTOR3* min, D3DXVECTOR3* max)
{
	out->x = get_random_float(min->x, max->x);
	out->y = get_random_float(min->y, max->y);
	out->z = get_random_float(min->z, max->z);
}
		void Emitter::initParticle(Particle& p, float t)
		{
			init_physics_parameters(p.initial);
			init_physics_parameters(p.current);
			p.initial.position = current.position;
			p.initial.color = getColor();
			p.initial.time_to_live = time_to_live_->getValue(technique_->getParticleSystem()->getElapsedTime());
			p.initial.velocity = velocity_->getValue(technique_->getParticleSystem()->getElapsedTime());
			p.initial.mass = mass_->getValue(technique_->getParticleSystem()->getElapsedTime());
			p.initial.dimensions = technique_->getDefaultDimensions();
			if(orientation_range_) {
				p.initial.orientation = glm::slerp(orientation_range_->first, orientation_range_->second, get_random_float(0.0f,1.0f));
			} else {
				p.initial.orientation = current.orientation;
			}
			p.initial.direction = getInitialDirection();
			p.emitted_by = this;
		}
示例#11
0
文件: random.cpp 项目: franaln/evolve
// get random probability: float between 0 and 1
float get_random_prob()
{
  return get_random_float(0.0, 1.0);
}
	el::Mat2x3Array2 texture_matrices;
	el::BitSet64 sRGB;
	glm::vec4 color;
	Uint32 i;

	BOOST_FOREACH(glm::mat2x3 &value, texture_matrices)
	{
		value[0][0] = get_random_float();
		value[0][1] = get_random_float();
		value[0][2] = get_random_float();
		value[1][0] = get_random_float();
		value[1][1] = get_random_float();
		value[1][2] = get_random_float();
	}

	color[0] = get_random_float();
	color[1] = get_random_float();
	color[2] = get_random_float();
	color[3] = get_random_float();

	BOOST_FOREACH(el::String &name, textures)
	{
		name = get_random_name();
	}

	for (i = 0; i < el::material_texture_count; ++i)
	{
		sRGB[i] = get_random_bool();
	}

	name = get_random_name();