Exemple #1
0
void
Test::testNormalDistribution()
{
    RandomGen rnd;

    int buckets[101];
    for (int b = 0; b < 101; b++) {
        buckets[b] = 0;
    }

    const int sum = 10000000;
    int oor = 0;
    for (int i = 0; i < sum; i++) {
        double foo = rnd.nextNormal(50, 13);
        int idx = (int)(foo+0.5);
        if (foo < 0) {
            ++oor;
            idx = 0;
        }
        if (foo > 100) {
            ++oor;
            idx = 100;
        }
        buckets[idx]++;
    }
    EXPECT_TRUE(oor < 0.001 * sum);
    printf("out of range of normal distribution: %d / %d\n", oor, sum);

    printf("histogram in form:\nbucket\tnum\n>>> begin >>>\n");
    for (int b = 0; b < 101; b++) {
        printf("%d\t%d\n", b, buckets[b]);
    }
    printf("<<< end histogram <<<\n");

    EXPECT_TRUE(buckets[50] > buckets[45]);
    EXPECT_TRUE(buckets[45] > buckets[40]);
    EXPECT_TRUE(buckets[40] > buckets[35]);
    EXPECT_TRUE(buckets[35] > buckets[30]);
    EXPECT_TRUE(buckets[30] > buckets[25]);
    EXPECT_TRUE(buckets[25] > buckets[20]);
    EXPECT_TRUE(buckets[20] > buckets[15]);
    EXPECT_TRUE(buckets[15] > buckets[10]);
    EXPECT_TRUE(buckets[10] > buckets[5]);
    EXPECT_TRUE(buckets[5]  > buckets[1]);

    EXPECT_TRUE(buckets[50] > buckets[55]);
    EXPECT_TRUE(buckets[55] > buckets[60]);
    EXPECT_TRUE(buckets[60] > buckets[65]);
    EXPECT_TRUE(buckets[65] > buckets[70]);
    EXPECT_TRUE(buckets[70] > buckets[75]);
    EXPECT_TRUE(buckets[75] > buckets[80]);
    EXPECT_TRUE(buckets[80] > buckets[85]);
    EXPECT_TRUE(buckets[85] > buckets[90]);
    EXPECT_TRUE(buckets[90] > buckets[95]);
    EXPECT_TRUE(buckets[95] > buckets[99]);

    // not too fat tails:
    EXPECT_TRUE(buckets[10] > buckets[0]);
    EXPECT_TRUE(buckets[90] > buckets[100]);
}
Exemple #2
0
Tile getSpecialCreatureSprite(const ViewObject& obj, bool humanoid) {
  RandomGen r;
  r.init(hash<string>()(obj.getBareDescription()));
  if (humanoid)
    return Tile::byCoord(r.get(7), 10);
  else
    return Tile::byCoord(r.get(7, 10), 10);
}
Exemple #3
0
Tile getSpecialCreature(const ViewObject& obj, bool humanoid) {
  RandomGen r;
  r.init(hash<string>()(obj.getBareDescription()));
  string let = humanoid ? "WETUIPLKJHFAXBM" : "qwetyupkfaxbnm";
  char c;
  if (contains(let, obj.getBareDescription()[0]))
    c = obj.getBareDescription()[0];
  else
  if (contains(let, tolower(obj.getBareDescription()[0])))
    c = tolower(obj.getBareDescription()[0]);
  else
    c = let[r.get(let.size())];
  return Tile::unicode(c, ColorId(Random.get(EnumInfo<ColorId>::getSize())));
}
Exemple #4
0
Tile getSpecialCreature(const ViewObject& obj, bool humanoid) {
  RandomGen r;
  r.init(hash<string>()(obj.getBareDescription()));
  string let = humanoid ? "WETUIPLKJHFAXBM" : "qwetyupkfaxbnm";
  char c;
  if (contains(let, obj.getBareDescription()[0]))
    c = obj.getBareDescription()[0];
  else
  if (contains(let, tolower(obj.getBareDescription()[0])))
    c = tolower(obj.getBareDescription()[0]);
  else
    c = let[r.getRandom(let.size())];
  Color col(r.getRandom(80, 250), r.getRandom(80, 250), 0);
  return Tile(c, col);
}
Exemple #5
0
void GameDesk::IconGen(QString path, QVector<bool> &players, RandomGen &generator)
{
    QVector<int> numbers;
    numbers.resize(6);

    for (int i = 0; i < board.size(); i += 6) {
        generator.gen6(numbers);
        for (int j=0; j<6; j++) {
          board[i + j]->SetIconName(path + QString("%1").arg(numbers[j] + 1) + ".png");
          board[i + j]->setIconNumber(numbers[j] + 1);
        }
    }

    GameSquare* square;
    GameSquare* home_square;
    for (int i = 0; i < MAX_PLAYERS; i++)
    {
      if (players[i])
      {
        for (int j = 0; j < 4; j++)
        { // preneseni nasledujicich nagenerovanych ikon z desky do domecku
          square = board[i*(board.size()/4)  + j];
          home_square = e_home_squares[i][j];
          home_square->SetIconName(square->IconName());
          home_square->setIconNumber(square->getIconNumber());
        }
      }
    }
  scene->invalidate();
}
FVec2 EmissionSource::sample(RandomGen &rand) const {
  switch (type) {
  case Type::point:
    return pos;
  case Type::rect: {
    auto f2 = rand.getFloat2Fast();
    return pos + FVec2((f2.first * 2.0f - 1.0f) * param.x, (f2.second * 2.0f - 1.0f) * param.y);
  }
  case Type::sphere: {
    FVec2 spoint(rand.getFloatFast(-1.0f, 1.0f), rand.getFloatFast(-1.0f, 1.0f));
    while(spoint.x * spoint.x + spoint.y * spoint.y > 1.0f)
      spoint = FVec2(rand.getFloatFast(-1.0f, 1.0f), rand.getFloatFast(-1.0f, 1.0f));
    return pos + spoint * param.x;
  }
  }

  return {};
}
static Table<Campaign::SiteInfo> getTerrain(RandomGen& random, Vec2 size, int numBlocked) {
  Table<Campaign::SiteInfo> ret(size, {});
  for (Vec2 v : ret.getBounds())
    ret[v].viewId.push_back(ViewId("grass"));
  vector<Vec2> freePos = ret.getBounds().getAllSquares();
  for (int i : Range(numBlocked)) {
    Vec2 pos = random.choose(freePos);
    freePos.removeElement(pos);
    ret[pos].setBlocked();
  }
  return ret;
}
Exemple #8
0
Object::Object(ObjectType *objectType, const Vec3f &pos, const Vec2i &mapPos) : BaseColorPickEntity() {
	RandomGen random;

	random.init(static_cast<int>(pos.x * pos.z));
	this->lastRenderFrame = 0;
	this->objectType= objectType;
	resource= NULL;
	highlight= 0.f;
	animated= false;
	this->mapPos = mapPos;
	this->pos= pos + Vec3f(random.randRange(-0.6f, 0.6f), 0.0f, random.randRange(-0.6f, 0.6f));
	rotation= random.randRange(0.f, 360.f);
	if(objectType!=NULL){
		variation = random.randRange(0, objectType->getModelCount()-1);
		TilesetModelType *tmt=objectType->getTilesetModelType(variation);
		if(tmt->getRotationAllowed() != true) {
			rotation=0;
		}
		if(tmt->getRandomPositionEnabled() != true) {
			this->pos = pos;
		}
		animated=tmt->getAnimSpeed()>0;
	}
	else {
		variation=0;
	}
	visible=false;
	animProgress=0.0f;
}
Exemple #9
0
void
Test::testFloatingPoint()
{
    RandomGen rnd;

    int buckets[100];
    for (int b = 0; b < 100; b++) {
        buckets[b] = 0;
    }
    for (int i = 0; i < 100000; i++) {
        double foo = rnd.nextDouble() * 100.0;
        int b = (int)foo;
        EXPECT_TRUE(b >= 0);
        EXPECT_TRUE(b < 100);
        if (b >= 0 && b < 100) ++buckets[b];
    }
    for (int b = 0; b < 100; b++) {
        // note that it's *possible* for this to fail:
        EXPECT_TRUE(buckets[b] > 800);
        EXPECT_TRUE(buckets[b] < 1200);
        // printf("bucket[%d] = %d\n", b, buckets[b]);
    }
}
Exemple #10
0
int main (int argc, char *argv[])
{

    int ncpu=4, ndim=3,i,nmode=31,init_rand=600;
    double mode[ndim][nmode];
    double fourforce[ndim][nmode];
    double projtens[ndim][ndim][nmode];
    int seed_gauss[ncpu][4];
    int forcseed[4];

    RandomGen randomGen = RandomGen();

    randomGen.rans(ncpu,init_rand,seed_gauss);

    for (int i=0; i<ncpu; i++) {

        printf("%d %d %d %d\n",
               seed_gauss[i][0],
               seed_gauss[i][1],
               seed_gauss[i][2],
               seed_gauss[i][3]);

    }

    forcseed[0] = seed_gauss[0][0];
    forcseed[1] = seed_gauss[0][1];
    forcseed[2] = seed_gauss[0][2];
    forcseed[3] = seed_gauss[0][3];

    for (int imode=0; imode<nmode; imode++) {
        double randomnumber;
        randomGen.gaussDev(forcseed, randomnumber);
        printf("%d %.20g\n",imode+1,randomnumber);
    }

    return 0;
}
Exemple #11
0
vector<Vec2> Vec2::box(int radius, bool shuffle) {
  if (radius == 0)
    return {*this};
  vector<Vec2> v;
  for (int k = -radius; k < radius; ++k)
    v.push_back(*this + Vec2(k, -radius));
  for (int k = -radius; k < radius; ++k)
    v.push_back(*this + Vec2(radius, k));
  for (int k = -radius; k < radius; ++k)
    v.push_back(*this + Vec2(-k, radius));
  for (int k = -radius; k < radius; ++k)
    v.push_back(*this + Vec2(-radius, -k));
  if (shuffle)
    random_shuffle(v.begin(), v.end(), [](int a) { return Random.getRandom(a);});
  return v;
}
Exemple #12
0
vector<Vec2> Vec2::directions4(RandomGen& random) {
  return random.permutation(directions4());
}
Exemple #13
0
void Embedding::randomGenerate(RandomGen& r){
	for (int i = 0; i < this->size; i++)
		this->elem[i] = (r.Random() - 0.5) * (1.0 / double(this->size));
}
Exemple #14
0
vector<Vec2> Vec2::neighbors4(bool shuffle) const {
  vector<Vec2> res = { Vec2(x, y + 1), Vec2(x + 1, y), Vec2(x, y - 1), Vec2(x - 1, y)};
  if (shuffle)
    random_shuffle(res.begin(), res.end(),[](int a) { return Random.getRandom(a);});
  return res;
}
Exemple #15
0
std::string randomAlphaNumeric(size_t len)
{
	return r.randomAlphaNumeric(len);
}
Exemple #16
0
uint64_t rand64()
{
	return r.rand64();
}
Exemple #17
0
vector<Vec2> Vec2::neighbors4(RandomGen& random) const {
  return random.permutation(neighbors4());
}
Exemple #18
0
void Svd<Real>::jacobi(Matrix& At, Vec& w, Vec_d& wd, optional<Matrix&> Vt, RandomGen& rand)
{
    int m = At.cols(), n = w.rows(), n1 = At.rows();
    Double eps = Double_::epsilon*10;
    int i, j, k, iter, max_iter = std::max(m, 30);
    int acols = At.cols(), vcols = Vt ? Vt->cols() : 0;
    Real c, s;
    Double sd;

    for( i = 0; i < n; i++ )
    {
        for( k = 0, sd = 0; k < m; k++ )
        {
            Real t = At(i,k);
            sd += (Double)t*t;
        }
        wd[i] = sd;
    }
    
    if (Vt) Vt->fromIdentity();

    for( iter = 0; iter < max_iter; iter++ )
    {
        bool changed = false;
        
        for( i = 0; i < n-1; i++ )
            for( j = i+1; j < n; j++ )
            {
                int Ai = i*acols, Aj = j*acols;
                Double a = wd[i], p = 0, b = wd[j];
                
                for( k = 0; k < m; k++ )
                    p += (Double)At(Ai+k)*At(Aj+k);
                
                if( Alge_d::abs(p) <= eps*Alge_d::sqrt((Double)a*b) )
                    continue;           
                
                p *= 2;
                Double beta = a - b, gamma = Alge_d::hypot((Double)p, beta), delta;
                if( beta < 0 )
                {
                    delta = (gamma - beta)*0.5;
                    s = (Real)Alge_d::sqrt(delta/gamma);
                    c = (Real)(p/(gamma*s*2));
                }
                else
                {
                    c = (Real)Alge_d::sqrt((gamma + beta)/(gamma*2));
                    s = (Real)(p/(gamma*c*2));
                    delta = p*p*0.5/(gamma + beta);
                }
                
                if( iter % 2 )
                {
                    wd[i] += delta;
                    wd[j] -= delta;
                    
                    for( k = 0; k < m; k++ )
                    {
                        Real t0 = c*At(Ai+k) + s*At(Aj+k);
                        Real t1 = -s*At(Ai+k) + c*At(Aj+k);
                        At(Ai+k) = t0; At(Aj+k) = t1;
                    }
                }
                else
                {
                    a = b = 0;
                    for( k = 0; k < m; k++ )
                    {
                        Real t0 = c*At(Ai+k) + s*At(Aj+k);
                        Real t1 = -s*At(Ai+k) + c*At(Aj+k);
                        At(Ai+k) = t0; At(Aj+k) = t1;
                        
                        a += (Double)t0*t0; b += (Double)t1*t1;
                    }
                    wd[i] = a; wd[j] = b;
                }
                
                changed = true;
                
                if( Vt )
                {
                    int Vi = i*vcols, Vj = j*vcols;
                    
                    for( k = 0; k < n; k++ )
                    {
                        Real t0 = c*(*Vt)(Vi+k) + s*(*Vt)(Vj+k);
                        Real t1 = -s*(*Vt)(Vi+k) + c*(*Vt)(Vj+k);
                        (*Vt)(Vi+k) = t0; (*Vt)(Vj+k) = t1;
                    }
                }
            }
        if( !changed )
            break;
    }
    
    for( i = 0; i < n; i++ )
    {
        for( k = 0, sd = 0; k < m; k++ )
        {
            Real t = At(i,k);
            sd += (Double)t*t;
        }
        wd[i] = Alge_d::sqrt(sd);
    }
    
    for( i = 0; i < n-1; i++ )
    {
        j = i;
        for( k = i+1; k < n; k++ )
        {
            if( wd[j] < wd[k] )
                j = k;
        }
        if( i != j )
        {
            std::swap(wd[i], wd[j]);
            if( Vt )
            {
                for( k = 0; k < m; k++ )
                    std::swap(At(i,k), At(j,k));
                
                for( k = 0; k < n; k++ )
                    std::swap((*Vt)(i,k), (*Vt)(j,k));
            }
        }
    }
    
    for( i = 0; i < n; i++ )
        w[i] = wd[i];

    if( !Vt )
        return;

    for( i = 0; i < n1; i++ )
    {
        sd = i < n ? wd[i] : 0;
        
        while( sd == 0 )
        {
            // if we got a zero singular value, then in order to get the corresponding left singular vector
            // we generate a random vector, project it to the previously computed left singular vectors,
            // subtract the projection and normalize the difference.
            const Real val0 = (Real)(1./m);
            for( k = 0; k < m; k++ )
            {
                Real val = (rand.next() & 256) != 0 ? val0 : -val0;
                At(i,k) = val;
            }
            for( iter = 0; iter < 2; iter++ )
            {
                for( j = 0; j < i; j++ )
                {
                    sd = 0;
                    for( k = 0; k < m; k++ )
                        sd += At(i,k)*At(j,k);
                    Real asum = 0;
                    for( k = 0; k < m; k++ )
                    {
                        Real t = (Real)(At(i,k) - sd*At(j,k));
                        At(i,k) = t;
                        asum += Alge::abs(t);
                    }
                    asum = asum ? 1/asum : 0;
                    for( k = 0; k < m; k++ )
                        At(i,k) *= asum;
                }
            }
            sd = 0;
            for( k = 0; k < m; k++ )
            {
                Real t = At(i,k);
                sd += (Double)t*t;
            }
            sd = Alge_d::sqrt(sd);
        }
        
        s = (Real)(1/sd);
        for( k = 0; k < m; k++ )
            At(i,k) *= s;
    }
}
Exemple #19
0
vector<Dweller> shuffle(RandomGen& random, vector<T> v) {
  random.shuffle(v.begin(), v.end());
  return v.transform([](const T& t) { return Dweller(t); });
}
Exemple #20
0
Vec2 Rectangle::randomVec2() const {
  return Vec2(Random.get(px, kx), Random.get(py, ky));
}
Exemple #21
0
optional<Campaign> Campaign::prepareCampaign(View* view, Options* options, RetiredGames&& retired,
    RandomGen& random) {
  Vec2 size(16, 9);
  int numBlocked = 0.6 * size.x * size.y;
  Table<SiteInfo> terrain = getTerrain(random, size, numBlocked);
  string worldName = NameGenerator::get(NameGeneratorId::WORLD)->getNext();
  options->setDefaultString(OptionId::KEEPER_NAME, NameGenerator::get(NameGeneratorId::FIRST)->getNext());
  while (1) {
    //options->setLimits(OptionId::RETIRED_VILLAINS, 0, min<int>(retired.size(), 4)); 
    options->setLimits(OptionId::MAIN_VILLAINS, 0, 9); 
    options->setLimits(OptionId::LESSER_VILLAINS, 0, 8); 
    options->setLimits(OptionId::ALLIES, 0, 6); 
    options->setLimits(OptionId::INFLUENCE_SIZE, 3, 6); 
    int numRetired = min(retired.getNumActive(), options->getIntValue(OptionId::MAIN_VILLAINS));
    int numMain = options->getIntValue(OptionId::MAIN_VILLAINS) - numRetired;
    int numLesser = options->getIntValue(OptionId::LESSER_VILLAINS);
    int numAllies = options->getIntValue(OptionId::ALLIES);
    vector<VillainInfo> mainVillains;
    while (mainVillains.size() < numMain)
      append(mainVillains, random.permutation(getMainVillains()));
    mainVillains.resize(numMain);
    vector<VillainInfo> lesserVillains;
    while (lesserVillains.size() < numLesser)
      append(lesserVillains, random.permutation(getLesserVillains()));
    lesserVillains.resize(numLesser);
    vector<VillainInfo> allies;
    while (allies.size() < numAllies)
      append(allies, random.permutation(getAllies()));
    allies.resize(numAllies);
    Campaign campaign(terrain);
    campaign.worldName = worldName;
    vector<Vec2> freePos;
    for (Vec2 v : Rectangle(size))
      if (campaign.sites[v].canEmbark())
        freePos.push_back(v);
    for (int i : All(mainVillains)) {
      Vec2 pos = random.choose(freePos);
      removeElement(freePos, pos);
      campaign.sites[pos].dweller = mainVillains[i];
    }
    vector<RetiredGames::RetiredGame> activeGames = retired.getActiveGames();
    for (int i : Range(numRetired)) {
      Vec2 pos = random.choose(freePos);
      removeElement(freePos, pos);
      campaign.sites[pos].dweller = RetiredInfo{activeGames[i].gameInfo, activeGames[i].fileInfo};
    }
    for (int i : All(lesserVillains)) {
      Vec2 pos = random.choose(freePos);
      removeElement(freePos, pos);
      campaign.sites[pos].dweller = lesserVillains[i];
    }
    for (int i : All(allies)) {
      Vec2 pos = random.choose(freePos);
      removeElement(freePos, pos);
      campaign.sites[pos].dweller = allies[i];
    }
    while (1) {
      bool updateMap = false;
      campaign.influenceSize = options->getIntValue(OptionId::INFLUENCE_SIZE);
      campaign.refreshInfluencePos();
      CampaignAction action = view->prepareCampaign(campaign, options, retired);
      switch (action.getId()) {
        case CampaignActionId::REROLL_MAP:
            terrain = getTerrain(random, size, numBlocked);
            worldName = NameGenerator::get(NameGeneratorId::WORLD)->getNext();
            options->setDefaultString(OptionId::KEEPER_NAME, NameGenerator::get(NameGeneratorId::FIRST)->getNext());
        case CampaignActionId::UPDATE_MAP:
            updateMap = true; break;
        case CampaignActionId::UPDATE_OPTION:
            switch (action.get<OptionId>()) {
              case OptionId::KEEPER_NAME:
              case OptionId::INFLUENCE_SIZE: break;
              default: updateMap = true; break;
            }
            break;
        case CampaignActionId::CANCEL:
            return none;
        case CampaignActionId::CHOOSE_SITE:
            if (campaign.playerPos)
              campaign.clearSite(*campaign.playerPos);
            campaign.playerPos = action.get<Vec2>();
            campaign.sites[*campaign.playerPos].dweller = PlayerInfo{ViewId::KEEPER};
            break;
        case CampaignActionId::CONFIRM:
            return campaign;
      }
      if (updateMap)
        break;
    }
  }
}