当前位置: 首页>>代码示例>>C++>>正文


C++ rand_range函数代码示例

本文整理汇总了C++中rand_range函数的典型用法代码示例。如果您正苦于以下问题:C++ rand_range函数的具体用法?C++ rand_range怎么用?C++ rand_range使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了rand_range函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: rand_range

npc::npc(dungeon_t *d, const monster_description &m)
{
  pair_t p;
  uint32_t room;

  symbol = m.symbol;
  color = m.color;
  room = rand_range(1, d->num_rooms - 1);
  do {
    p[dim_y] = rand_range(d->rooms[room].position[dim_y],
                          (d->rooms[room].position[dim_y] +
                           d->rooms[room].size[dim_y] - 1));
    p[dim_x] = rand_range(d->rooms[room].position[dim_x],
                          (d->rooms[room].position[dim_x] +
                           d->rooms[room].size[dim_x] - 1));
  } while (d->charmap[p[dim_y]][p[dim_x]]);
  pc_last_known_position[dim_y] = p[dim_y];
  pc_last_known_position[dim_x] = p[dim_x];
  position[dim_y] = p[dim_y];
  position[dim_x] = p[dim_x];
  d->charmap[p[dim_y]][p[dim_x]] = this;
  speed = m.speed.roll();
  hp = m.hitpoints.roll();
  damage = &m.damage;
  bounty = (hp + damage->roll()) / 10; //Bounty
  level = m.level;
  next_turn = d->the_pc->next_turn;
  alive = 1;
  sequence_number = ++d->character_sequence_number;
  characteristics = m.abilities;
  have_seen_pc = 0;
  name = m.name.c_str();
  description = (const char *) m.description.c_str();
}
开发者ID:NotGoodAtCoding,项目名称:CS327,代码行数:34,代码来源:npc.cpp

示例2: tm_rect_generate_overlapping

void tm_rect_generate_overlapping( tm_rect_t* r, tm_rect_t* overlap_r, tm_vect_t* sz, vect_t* map_sz )
{
	tm_vect_t* unit = tm_vect_create(1, 1);
	tm_rect_t aux_r;

	tm_vect_substract( &overlap_r->v1, sz, &aux_r.v1 );
	tm_vect_add( &aux_r.v1, unit, &aux_r.v1 );
	if( (int) aux_r.v1.x < 0 )
		aux_r.v1.x = 0;
	if( (int) aux_r.v1.y < 0 )
		aux_r.v1.y = 0;

	tm_vect_substract( &overlap_r->v2, unit, &aux_r.v2 );
	tm_vect_add( &aux_r.v2, sz, &aux_r.v2 );
	if( (int) aux_r.v2.x > map_sz->x )
		aux_r.v2.x = map_sz->x;
	if( (int) aux_r.v2.y > map_sz->y )
		aux_r.v2.y = map_sz->y;

	assert( rect_is_valid(&aux_r) );

	tm_vect_init( &r->v1, rand_range((int)aux_r.v1.x, (int)aux_r.v2.x), rand_range((int)aux_r.v1.y, (int)aux_r.v2.y) );
	tm_vect_add( &r->v1, sz, &r->v2 );

	tm_vect_destroy( unit );
}
开发者ID:mikedw,项目名称:synquake_tsx,代码行数:26,代码来源:tm_geometry.c

示例3: mon_create_drop_count

/**
 * Return the number of things dropped by a monster.
 *
 * \param race is the monster race.
 * \param maximize should be set to false for a random number, true to find
 * out the maximum count.
 */
int mon_create_drop_count(const struct monster_race *race, bool maximize)
{
	int number = 0;
	static const int drop_4_max = 6;
	static const int drop_3_max = 4;
	static const int drop_2_max = 3;

	if (maximize) {
		if (rf_has(race->flags, RF_DROP_20)) number++;
		if (rf_has(race->flags, RF_DROP_40)) number++;
		if (rf_has(race->flags, RF_DROP_60)) number++;
		if (rf_has(race->flags, RF_DROP_4)) number += drop_4_max;
		if (rf_has(race->flags, RF_DROP_3)) number += drop_3_max;
		if (rf_has(race->flags, RF_DROP_2)) number += drop_2_max;
		if (rf_has(race->flags, RF_DROP_1)) number++;
	} else {
		if (rf_has(race->flags, RF_DROP_20) && randint0(100) < 20) number++;
		if (rf_has(race->flags, RF_DROP_40) && randint0(100) < 40) number++;
		if (rf_has(race->flags, RF_DROP_60) && randint0(100) < 60) number++;
		if (rf_has(race->flags, RF_DROP_4)) number += rand_range(2, drop_4_max);
		if (rf_has(race->flags, RF_DROP_3)) number += rand_range(2, drop_3_max);
		if (rf_has(race->flags, RF_DROP_2)) number += rand_range(1, drop_2_max);
		if (rf_has(race->flags, RF_DROP_1)) number++;
	}

	return number;
}
开发者ID:datatypevoid,项目名称:angband,代码行数:34,代码来源:mon-make.c

示例4: main

int main(int argc, char** argv) {
  lightDirection.normalize();

  srand((unsigned) time(NULL));

  // 球体をランダムに配置
  for(int i = 0; i < sphere_n; i++) {
    double r = rand_range(50,200);
    Vector3d center(rand_range(-500,500), r, rand_range(-1000,0));
    //Vector3d color = HSVtoRGB( (360.0/sphere_n)*i, 255, 255);
    Vector3d color = HSVtoRGB( rand_range(0,360), 255, 255);
    sphere[i] = Sphere(center, r, color);
  }

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(600,600);
  glutInitWindowPosition(180,10);
  glutCreateWindow(argv[0]);
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glShadeModel(GL_FLAT);

  glutDisplayFunc(display);
  glutReshapeFunc(resizeWindow);
  glutKeyboardFunc(keyboard);
  glutMainLoop();

  return 0;
}
开发者ID:gam0022,项目名称:computer-graphics,代码行数:29,代码来源:q9_5.cpp

示例5: make_map

static void
make_map(void) {
    int w = WIDTH;
    int h = HEIGHT;
    float deviance;

    //Reset the whole heightmap to the minimum height
    for (int e = 0; e < HEIGHT; ++e)
        for (int i = 0; i < WIDTH; ++i)
            heightmap[i][e] = MINHEIGHT;

    //Add our starting corner points
    heightmap[0][0] = rand_range(-RANGE_CHANGE, RANGE_CHANGE);
    heightmap[0][HEIGHT] = rand_range(-RANGE_CHANGE, RANGE_CHANGE);

    deviance = 1.0;
    while (1) {
        draw_all_squares(w, h, deviance);
        draw_all_diamonds(w, h, deviance);

        w /= 2;
        h /= 2;

        if (w < 2 && h < 2)
            break;

        if (w < 2)
            w = 2;
        if (h < 2)
            h = 2;
        deviance *= REDUCTION;
    }
}
开发者ID:0xAether,项目名称:Fractal-Terrain-Test,代码行数:33,代码来源:frac.c

示例6: pred_mutate

/**
 * Genome mutation function
 *
 * Recalculates phenotype if necessary
 *
 * @param  genes
 * @return
 */
void pred_mutate(pred_genome_t genome)
{
    int max_changed_genes = _metadata->mutation_rate * _metadata->genotype_length;
    int genes_to_change = rand_range(0, max_changed_genes);

    for (int i = 0; i < genes_to_change; i++) {
        // choose mutated gene
        int gene = rand_range(0, _metadata->genotype_length - 1);
        pred_gene_t old_value = genome->_genes[gene];

        // generate new value
        pred_gene_t value = rand_urange(0, _metadata->max_gene_value);
        if (_metadata->genome_type == permuted) {
            // either unused or same value is valid, so make corrections
            while(genome->_used_values[value] && old_value != value) {
                value = (value + 1) % (_metadata->max_gene_value + 1);
            };
        }

        // rewrite gene
        genome->_genes[gene] = value;
        genome->_used_values[value] = true;
    }

    pred_calculate_phenotype(genome);
}
开发者ID:xurban46,项目名称:bc,代码行数:34,代码来源:predictors.c

示例7: io_teleport_pc

uint32_t io_teleport_pc(dungeon_t *d)
{
  /* Just for fun. */
  pair_t dest;

  do {
    dest[dim_x] = rand_range(1, DUNGEON_X - 2);
    dest[dim_y] = rand_range(1, DUNGEON_Y - 2);
  } while (charpair(dest));

  d->charmap[character_get_y(d->pc)][character_get_x(d->pc)] = NULL;
  d->charmap[dest[dim_y]][dest[dim_x]] = d->pc;

  character_set_y(d->pc, dest[dim_y]);
  character_set_x(d->pc, dest[dim_x]);

  if (mappair(dest) < ter_floor) {
    mappair(dest) = ter_floor;
  }

  pc_observe_terrain(d->pc, d);

  dijkstra(d);
  dijkstra_tunnel(d);

  return 0;
}
开发者ID:NotGoodAtCoding,项目名称:CS327,代码行数:27,代码来源:io.cpp

示例8: prepare_sprite

// converts a dead person in black particles
void Person::burn()
{
  GamePlay *gameplay = GAMEPLAY;
  BITMAP *sprite = prepare_sprite();
  int x, y, u, v;

  gameplay->get_level()->to_screen(m_pos, x, y);

  x = x-TILE_W/2;
  y = y-TILE_H+1;

  for (v=0; v<sprite->h; ++v)
    for (u=0; u<sprite->w; ++u) {
      int color, ou;

      if (m_right)
	color = getpixel(sprite, ou=u, v);
      else
	color = getpixel(sprite, ou=sprite->w-1-u, v);

      if (color != bitmap_mask_color(sprite))
	gameplay->add_particle
	  (new PixelParticle(vector2d((m_pos.x*TILE_W - TILE_W/2 + ou) / TILE_W,
				      (m_pos.y*TILE_H - TILE_H+1 + v) / TILE_H),
			     vector2d(0, rand_range(-2.0, -0.1)),
			     vector2d(rand_range(0.5, 4.0), 0),
			     rand_range(BPS/4, BPS*3/4),
			     makecol(0, 0, 0),
			     makecol(0, 0, 0)));

    }
}
开发者ID:dacap,项目名称:defenderofnothing,代码行数:33,代码来源:person.cpp

示例9: while

    void Snake::setAppleLocation() {
        if (!needFood)
            return;
        int x;
        int y;

        while(true){

            x = rand_range(10, wW - 10);
            if (x % 10 != 0)
                x -= (x % 10);

            y = rand_range(windowOffset, wH - 10);
            if (y % 10 != 0)
                y -= (y % 10);

            bool allowed = true;
            for (std::list<Location>::const_iterator it = locations.begin(), end = locations.end(); it != end; ++it) {
                if ((*it).x == x && (*it).y == y){
                    allowed = false;
                    break;
                }
            }
            if (allowed)
                break;
        }


        foodLocation.x = x;
        foodLocation.y = y;
        needFood = false;

    }
开发者ID:taganaka,项目名称:snake,代码行数:33,代码来源:snake.cpp

示例10: generate_world

void generate_world(World* world, Tile_Info* info, isize ti_count, uint64 seed, Memory_Arena* arena)
{
	Random r_s;
	Random* r = &r_s;
	init_random(r, seed);


	for(isize i = 0; i < world->areas_height; ++i) {
		for(isize j = 0; j < world->areas_width; ++j) {
			isize index = i * world->areas_width + j;
			World_Area* area = world->areas + index;
			init_world_area(area, arena);
			area->map.info = info;
			area->map.info_count = ti_count;
			generate_tilemap(&area->map, next_random_uint64(r));
			for(isize i = 0; i < World_Area_Tilemap_Width; ++i) {
				Entity* e = world_area_get_next_entity(area);
				Sim_Body* b = sim_find_body(&area->sim, e->body_id);
				e->sprite.texture = Get_Texture_Coordinates(0, 96, 32, 64);
				b->shape.hw = 16;
				b->shape.hh = 12;
				b->inv_mass = 1.0f;
				e->sprite.size = v2(32, 64);
				e->sprite.center = v2(0, 20);
				entity_add_event_on_activate(e, test_on_activate);
				do {
					b->shape.center = v2(
						rand_range(r, 0, area->map.w * 32),
						rand_range(r, 0, area->map.h * 32));
				}
				while (info[tilemap_get_at(&area->map, b->shape.center)].solid);
			}

			generate_statics_for_tilemap(&area->sim, &area->map);

			isize north_link = modulus(i - 1, world->areas_height) * world->areas_width + j;
			isize south_link = modulus(i + 1, world->areas_height) * world->areas_width + j;
			isize west_link = i * world->areas_width + modulus(j - 1, world->areas_width);
			isize east_link = i * world->areas_width + modulus(j + 1, world->areas_width);
			area->north = Area_Link {
				v2i(World_Area_Tilemap_Width / 2,  World_Area_Tilemap_Height - 1), 
				world->areas + north_link
			};
			area->south = Area_Link {
				v2i(World_Area_Tilemap_Width / 2, 1),
				world->areas + south_link
			};
			area->west = Area_Link {
				v2i(World_Area_Tilemap_Width - 1, World_Area_Tilemap_Height / 2),
				world->areas + west_link
			};
			area->east = Area_Link {
				v2i(1, World_Area_Tilemap_Height / 2),
				world->areas + east_link
			};
		}
	}

}
开发者ID:CHMyFork,项目名称:rituals-game,代码行数:59,代码来源:rituals_world.cpp

示例11: paintLight

void paintLight(lightSource *theLight, short x, short y, boolean isMinersLight) {
	short i, j, k;
	short colorComponents[3], randComponent, lightMultiplier, thisComponent;
	short fadeToPercent;
	float radius;
	char grid[DCOLS][DROWS];
	boolean dispelShadows;
	
#ifdef BROGUE_ASSERTS
	assert(rogue.RNG == RNG_SUBSTANTIVE);
#endif
	
	radius = randClump(theLight->lightRadius);
	radius /= 100;
	
	randComponent = rand_range(0, theLight->lightColor->rand);
	colorComponents[0] = randComponent + theLight->lightColor->red + rand_range(0, theLight->lightColor->redRand);
	colorComponents[1] = randComponent + theLight->lightColor->green + rand_range(0, theLight->lightColor->greenRand);
	colorComponents[2] = randComponent + theLight->lightColor->blue + rand_range(0, theLight->lightColor->blueRand);
	
	// the miner's light does not dispel IS_IN_SHADOW,
	// so the player can be in shadow despite casting his own light.
	dispelShadows = !isMinersLight && colorComponents[0] + colorComponents[1] + colorComponents[2] > 0;
	
	fadeToPercent = theLight->radialFadeToPercent;
	
	// zero out only the relevant rectangle of the grid
	for (i = max(0, x - radius); i < DCOLS && i < x + radius; i++) {
		for (j = max(0, y - radius); j < DROWS && j < y + radius; j++) {
			grid[i][j] = 0;
		}
	}
	
	getFOVMask(grid, x, y, radius, T_OBSTRUCTS_VISION, (theLight->passThroughCreatures ? 0 : (HAS_MONSTER | HAS_PLAYER)),
			   (!isMinersLight));
	
	for (i = max(0, x - radius); i < DCOLS && i < x + radius; i++) {
		for (j = max(0, y - radius); j < DROWS && j < y + radius; j++) {
			if (grid[i][j]) {
				lightMultiplier = 100 - (100 - fadeToPercent) * (sqrt((i-x) * (i-x) + (j-y) * (j-y)) / (radius));
				for (k=0; k<3; k++) {
						thisComponent = colorComponents[k] * lightMultiplier / 100;
						tmap[i][j].light[k] += thisComponent;
				}
				if (dispelShadows) {
					pmap[i][j].flags &= ~IS_IN_SHADOW;
				}
			}
		}
	}
	
	tmap[x][y].light[0] += colorComponents[0];
	tmap[x][y].light[1] += colorComponents[1];
	tmap[x][y].light[2] += colorComponents[2];
	
	if (dispelShadows) {
		pmap[x][y].flags &= ~IS_IN_SHADOW;
	}
}
开发者ID:alunbestor,项目名称:Brogue,代码行数:59,代码来源:Light.c

示例12: lerp

void PE_Torpedo_Explosion::emit_single_particle(Particle& p)
{
  p.cur_pos = lerp(RAND_0_1,m_p1,m_p2) + vec2(RAND_0_1 * 0.002f, RAND_0_1 * 0.002f);
	p.cur_speed = normalized(vec2(RAND_1_1,RAND_1_1))*rand_range(0.01,0.1);
	p.cur_size = 0.0;
  float col = rand_range(0.0,0.1);
	p.cur_color = vec4(col,col,col,1);
}
开发者ID:basman,项目名称:Sea-Defender,代码行数:8,代码来源:pe_torpedo_expl.cpp

示例13: main

int main()
{
  char keyboard_input[100];
  XColor black_col,white_col,red_col,green_col,blue_col,yellow_col,magenta_col,cyan_col;
  Colormap colormap;
  char black_bits[] = "#000000";
  char white_bits[] = "#FFFFFF";    // Mix red, green and blue to get white
  char red_bits[] = "#FF0000";
  char green_bits[] = "#00FF00";
  char blue_bits[] = "#0000FF";
  char yellow_bits[] = "#FFFF00";   // Mix red and green to get yellow
  char magenta_bits[] = "#FF00FF";  // A sort of purple color
  char cyan_bits[] = "#00FFFF";     // A blue-green color

  //Display *dpy = XOpenDisplay(NIL); 
  dpy = XOpenDisplay(NIL); assert(dpy);   // Open the display
  // Define the colors we want to use
  colormap = DefaultColormap(dpy, 0);
  XParseColor(dpy, colormap, black_bits, &black_col); XAllocColor(dpy, colormap, &black_col);
  XParseColor(dpy, colormap, white_bits, &white_col); XAllocColor(dpy, colormap, &white_col);
  XParseColor(dpy, colormap, red_bits, &red_col); XAllocColor(dpy, colormap, &red_col);
  XParseColor(dpy, colormap, green_bits, &green_col);XAllocColor(dpy, colormap, &green_col);
  XParseColor(dpy, colormap, blue_bits, &blue_col);XAllocColor(dpy, colormap, &blue_col);
  XParseColor(dpy, colormap, yellow_bits, &yellow_col);XAllocColor(dpy, colormap, &yellow_col);
  XParseColor(dpy, colormap, magenta_bits, &magenta_col);XAllocColor(dpy, colormap, &magenta_col);
  XParseColor(dpy, colormap, cyan_bits, &cyan_col);XAllocColor(dpy, colormap, &cyan_col);
  // Create the window  The numbers are the x and y locations on the screen, the width and height, 
  // border width (which is usually zero)
  w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, window_width, window_height,
  0, black_col.pixel, black_col.pixel);
  XSelectInput(dpy, w, StructureNotifyMask);        // We want to get MapNotify events
  XMapWindow(dpy, w);         // "Map" the window (that is, make it appear on the screen)
  for(;;){XEvent e; XNextEvent(dpy,&e); if(e.type == MapNotify) break;} //Wait for the MapNotify event
  // which means that the window has appeared on the screen.
  gc = XCreateGC(dpy, w, 0, NIL);        // Create a "Graphics Context"
  get_pen(white_col);
  //  We are finally ready to do some drawing!  Whew!  

  // ................  Students: you put your beautiful code HERE: .......................
  srand(time(NULL)); //  Get ready to make random numbers.  The time "seeds" the generator.
  int i;
  double x=0,  y=0;
  home();      // Send the turtle to the middle of the window.  That's its "home"
  get_pen(green_col);  pen_down();
  for (i=0; i<36; i++) {
    x = x + rand_range(-30.0, 30.0);  //Generates random number between these two numbers.
    y = y + rand_range(-30.0, 30.0)+5.0;
    gotoxy(x, y);   // Move the turtle to the new location.  It will draw a line if the pen is down.
    XFlush(dpy); // Tell the graphics server that it should really show us the results now.
    printf("1 x=%g y=%g heading=%g\n",turtle_x,turtle_y,turtle_heading); //Optional: use for debugging
    usleep(100000); XFlush(dpy); // Optional: use this to see the lines being drawn, one-by-one
  }
  XFlush(dpy); // Tell the graphics server that it should really show us the results now.
  sleep(1);  // Wait for 1 second
  printf("Press enter when done.\n");
  fgets (keyboard_input,100,stdin); 
  return(0);
}
开发者ID:Vikas-Kaushik,项目名称:algorithms,代码行数:58,代码来源:copied_example3.cpp

示例14: relocate_targets

void relocate_targets(){
	
    for (int i=0; i<max_targets; i++) {
        targets[i].x=rand_range(100,getDisplayWidth()-100);
        targets[i].y=rand_range(100,getDisplayHeight()-100);
    }

    reassign_flares();	
}
开发者ID:armageddon421,项目名称:gles2framework,代码行数:9,代码来源:test.c

示例15: normal_value

XiEta normal_value(double mu, double sigma){
// Returns two normaly distributed, independent values with mu=0 and sigma=1
// based on the Box-Muller Method
  double u = rand_range(0,1);
  double v = rand_range(0,1);

  XiEta x = {mu + sigma*sqrt(-2*log(u))*cos(2*PI*v), mu + sigma*sqrt(-2*log(u))*sin(2*PI*v)};
  return x;
}
开发者ID:Marcello-Sega,项目名称:bachelorthesis,代码行数:9,代码来源:neighbor_list_force.c


注:本文中的rand_range函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。