예제 #1
0
int QgsPointSample::createRandomPoints( QProgressDialog* pd )
{
  Q_UNUSED( pd );

  //create input layer from id (test if polygon, valid)
  if ( !mInputLayer )
  {
    return 1;
  }

  if ( mInputLayer->geometryType() != QgsWkbTypes::PolygonGeometry )
  {
    return 2;
  }

  //delete output file if it already exists
  if ( QFile::exists( mOutputLayer ) )
  {
    QgsVectorFileWriter::deleteShapeFile( mOutputLayer );
  }

  //create vector file writer
  QgsFields outputFields;
  outputFields.append( QgsField( QStringLiteral( "id" ), QVariant::Int ) );
  outputFields.append( QgsField( QStringLiteral( "station_id" ), QVariant::Int ) );
  outputFields.append( QgsField( QStringLiteral( "stratum_id" ), QVariant::Int ) );
  QgsVectorFileWriter writer( mOutputLayer, QStringLiteral( "UTF-8" ),
                              outputFields,
                              QgsWkbTypes::Point,
                              mInputLayer->crs() );

  //check if creation of output layer successful
  if ( writer.hasError() != QgsVectorFileWriter::NoError )
  {
    return 3;
  }

  //init random number generator
  mt_srand( QTime::currentTime().msec() );
  QgsFeature fet;
  int nPoints = 0;
  double minDistance = 0;
  mNCreatedPoints = 0;

  QgsFeatureIterator fIt = mInputLayer->getFeatures( QgsFeatureRequest().setSubsetOfAttributes(
                             QStringList() << mNumberOfPointsAttribute << mMinDistanceAttribute, mInputLayer->fields() ) );
  while ( fIt.nextFeature( fet ) )
  {
    nPoints = fet.attribute( mNumberOfPointsAttribute ).toInt();
    if ( !mMinDistanceAttribute.isEmpty() )
    {
      minDistance = fet.attribute( mMinDistanceAttribute ).toDouble();
    }
    addSamplePoints( fet, writer, nPoints, minDistance );
  }

  return 0;
}
예제 #2
0
파일: random.c 프로젝트: Fleurer/mruby
static mrb_value mrb_random_mt_srand(mrb_state *mrb, mt_state *t, mrb_value seed)
{ 
  if (mrb_nil_p(seed)) {
    seed = mrb_fixnum_value(time(NULL) + mt_rand(t));
    if (mrb_fixnum(seed) < 0) {
      seed = mrb_fixnum_value( 0 - mrb_fixnum(seed));
    }
  }

  mt_srand(t, (unsigned) mrb_fixnum(seed));

  return seed;
}
예제 #3
0
boolean nh_start_game(int fd, const char *name, int irole, int irace, int igend,
		      int ialign, enum nh_game_modes playmode)
{
    unsigned int seed = 0;
    
    if (!api_entry_checkpoint())
	return FALSE; /* init failed; programmer error! */

    if (fd == -1 || !name || !*name)
	goto err_out;
    
    if (!program_state.restoring) {
	turntime = (unsigned long long)time(NULL);
	seed = turntime ^ get_seedval();
	/* initialize the random number generator */
	mt_srand(seed);
    } /* else: turntime and rng seeding are done in logreplay.c */
    
    startup_common(name, playmode);
    
    if (!validrole(irole) || !validrace(irole, irace) ||
	!validgend(irole, irace, igend) || !validalign(irole, irace, ialign))
	goto err_out;
    u.initrole = irole; u.initrace = irace;
    u.initgend = igend; u.initalign = ialign;

    /* write out a new logfile header "NHGAME ..." with all the initial details */
    log_init();
    log_newgame(fd, turntime, seed, playmode);

    newgame();
    was_on_elbereth = !sengr_at("Elbereth", u.ux, u.uy); /* force botl update later */
    wd_message();

    api_exit();
    return TRUE;
    
err_out:
    api_exit();
    return FALSE;
}
예제 #4
0
int QgsTransectSample::createSample( QProgressDialog* pd )
{
  Q_UNUSED( pd );

  if ( !mStrataLayer || !mStrataLayer->isValid() )
  {
    return 1;
  }

  if ( !mBaselineLayer || !mBaselineLayer->isValid() )
  {
    return 2;
  }

  //stratum id is not necessarily an integer
  QVariant::Type stratumIdType = QVariant::Int;
  if ( !mStrataIdAttribute.isEmpty() )
  {
    stratumIdType = mStrataLayer->pendingFields().field( mStrataIdAttribute ).type();
  }

  //create vector file writers for output
  QgsFields outputPointFields;
  outputPointFields.append( QgsField( "id", stratumIdType ) );
  outputPointFields.append( QgsField( "station_id", QVariant::Int ) );
  outputPointFields.append( QgsField( "stratum_id", stratumIdType ) );
  outputPointFields.append( QgsField( "station_code", QVariant::String ) );
  outputPointFields.append( QgsField( "start_lat", QVariant::Double ) );
  outputPointFields.append( QgsField( "start_long", QVariant::Double ) );

  QgsVectorFileWriter outputPointWriter( mOutputPointLayer, "utf-8", outputPointFields, QGis::WKBPoint,
                                         &( mStrataLayer->crs() ) );
  if ( outputPointWriter.hasError() != QgsVectorFileWriter::NoError )
  {
    return 3;
  }

  outputPointFields.append( QgsField( "bearing", QVariant::Double ) ); //add bearing attribute for lines
  QgsVectorFileWriter outputLineWriter( mOutputLineLayer, "utf-8", outputPointFields, QGis::WKBLineString,
                                        &( mStrataLayer->crs() ) );
  if ( outputLineWriter.hasError() != QgsVectorFileWriter::NoError )
  {
    return 4;
  }

  QgsFields usedBaselineFields;
  usedBaselineFields.append( QgsField( "stratum_id", stratumIdType ) );
  usedBaselineFields.append( QgsField( "ok", QVariant::String ) );
  QgsVectorFileWriter usedBaselineWriter( mUsedBaselineLayer, "utf-8", usedBaselineFields, QGis::WKBLineString,
                                          &( mStrataLayer->crs() ) );
  if ( usedBaselineWriter.hasError() != QgsVectorFileWriter::NoError )
  {
    return 5;
  }

  //debug: write clipped buffer bounds with stratum id to same directory as out_point
  QFileInfo outputPointInfo( mOutputPointLayer );
  QString bufferClipLineOutput = outputPointInfo.absolutePath() + "/out_buffer_clip_line.shp";
  QgsFields bufferClipLineFields;
  bufferClipLineFields.append( QgsField( "id", stratumIdType ) );
  QgsVectorFileWriter bufferClipLineWriter( bufferClipLineOutput, "utf-8", bufferClipLineFields, QGis::WKBLineString, &( mStrataLayer->crs() ) );

  //configure distanceArea depending on minDistance units and output CRS
  QgsDistanceArea distanceArea;
  distanceArea.setSourceCrs( mStrataLayer->crs().srsid() );
  if ( mMinDistanceUnits == Meters )
  {
    distanceArea.setEllipsoidalMode( true );
  }
  else
  {
    distanceArea.setEllipsoidalMode( false );
  }

  //possibility to transform output points to lat/long
  QgsCoordinateTransform toLatLongTransform( mStrataLayer->crs(), QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ) );

  //init random number generator
  mt_srand( QTime::currentTime().msec() );

  QgsFeatureRequest fr;
  fr.setSubsetOfAttributes( QStringList() << mStrataIdAttribute << mMinDistanceAttribute << mNPointsAttribute, mStrataLayer->pendingFields() );
  QgsFeatureIterator strataIt = mStrataLayer->getFeatures( fr );

  QgsFeature fet;
  int nTotalTransects = 0;
  int nFeatures = 0;

  if ( pd )
  {
    pd->setMaximum( mStrataLayer->featureCount() );
  }

  while ( strataIt.nextFeature( fet ) )
  {
    if ( pd )
    {
      pd->setValue( nFeatures );
    }
    if ( pd && pd->wasCanceled() )
    {
      break;
    }

    if ( !fet.constGeometry() )
    {
      continue;
    }
    const QgsGeometry* strataGeom = fet.constGeometry();

    //find baseline for strata
    QVariant strataId = fet.attribute( mStrataIdAttribute );
    QgsGeometry* baselineGeom = findBaselineGeometry( strataId.isValid() ? strataId : -1 );
    if ( !baselineGeom )
    {
      continue;
    }

    double minDistance = fet.attribute( mMinDistanceAttribute ).toDouble();
    double minDistanceLayerUnits = minDistance;
    //if minDistance is in meters and the data in degrees, we need to apply a rough conversion for the buffer distance
    double bufferDist = bufferDistance( minDistance );
    if ( mMinDistanceUnits == Meters && mStrataLayer->crs().mapUnits() == QGis::DecimalDegrees )
    {
      minDistanceLayerUnits = minDistance / 111319.9;
    }

    QgsGeometry* clippedBaseline = strataGeom->intersection( baselineGeom );
    if ( !clippedBaseline || clippedBaseline->wkbType() == QGis::WKBUnknown )
    {
      delete clippedBaseline;
      continue;
    }
    QgsGeometry* bufferLineClipped = clipBufferLine( strataGeom, clippedBaseline, bufferDist );
    if ( !bufferLineClipped )
    {
      delete clippedBaseline;
      continue;
    }

    //save clipped baseline to file
    QgsFeature blFeature;
    blFeature.setGeometry( *clippedBaseline );
    blFeature.setAttribute( "stratum_id", strataId );
    blFeature.setAttribute( "ok", "f" );
    usedBaselineWriter.addFeature( blFeature );

    //start loop to create random points along the baseline
    int nTransects = fet.attribute( mNPointsAttribute ).toInt();
    int nCreatedTransects = 0;
    int nIterations = 0;
    int nMaxIterations = nTransects * 50;

    QgsSpatialIndex sIndex; //to check minimum distance
    QMap< QgsFeatureId, QgsGeometry* > lineFeatureMap;

    while ( nCreatedTransects < nTransects && nIterations < nMaxIterations )
    {
      double randomPosition = (( double )mt_rand() / MD_RAND_MAX ) * clippedBaseline->length();
      QgsGeometry* samplePoint = clippedBaseline->interpolate( randomPosition );
      ++nIterations;
      if ( !samplePoint )
      {
        continue;
      }
      QgsPoint sampleQgsPoint = samplePoint->asPoint();
      QgsPoint latLongSamplePoint = toLatLongTransform.transform( sampleQgsPoint );

      QgsFeature samplePointFeature;
      samplePointFeature.setGeometry( samplePoint );
      samplePointFeature.setAttribute( "id", nTotalTransects + 1 );
      samplePointFeature.setAttribute( "station_id", nCreatedTransects + 1 );
      samplePointFeature.setAttribute( "stratum_id", strataId );
      samplePointFeature.setAttribute( "station_code", strataId.toString() + "_" + QString::number( nCreatedTransects + 1 ) );
      samplePointFeature.setAttribute( "start_lat", latLongSamplePoint.y() );
      samplePointFeature.setAttribute( "start_long", latLongSamplePoint.x() );

      //find closest point on clipped buffer line
      QgsPoint minDistPoint;

      int afterVertex;
      if ( bufferLineClipped->closestSegmentWithContext( sampleQgsPoint, minDistPoint, afterVertex ) < 0 )
      {
        continue;
      }

      //bearing between sample point and min dist point (transect direction)
      double bearing = distanceArea.bearing( sampleQgsPoint, minDistPoint ) / M_PI * 180.0;

      QgsPolyline sampleLinePolyline;
      QgsPoint ptFarAway( sampleQgsPoint.x() + ( minDistPoint.x() - sampleQgsPoint.x() ) * 1000000,
                          sampleQgsPoint.y() + ( minDistPoint.y() - sampleQgsPoint.y() ) * 1000000 );
      QgsPolyline lineFarAway;
      lineFarAway << sampleQgsPoint << ptFarAway;
      QgsGeometry* lineFarAwayGeom = QgsGeometry::fromPolyline( lineFarAway );
      QgsGeometry* lineClipStratum = lineFarAwayGeom->intersection( strataGeom );
      if ( !lineClipStratum )
      {
        delete lineFarAwayGeom; delete lineClipStratum;
        continue;
      }

      //cancel if distance between sample point and line is too large (line does not start at point
      if ( lineClipStratum->distance( *samplePoint ) > 0.000001 )
      {
        delete lineFarAwayGeom; delete lineClipStratum;
        continue;
      }

      //if lineClipStratum is a multiline, take the part line closest to sampleQgsPoint
      if ( lineClipStratum->wkbType() == QGis::WKBMultiLineString
           || lineClipStratum->wkbType() == QGis::WKBMultiLineString25D )
      {
        QgsGeometry* singleLine = closestMultilineElement( sampleQgsPoint, lineClipStratum );
        if ( singleLine )
        {
          delete lineClipStratum;
          lineClipStratum = singleLine;
        }
      }

      //cancel if length of lineClipStratum is too small
      double transectLength = distanceArea.measure( lineClipStratum );
      if ( transectLength < mMinTransectLength )
      {
        delete lineFarAwayGeom; delete lineClipStratum;
        continue;
      }

      //search closest existing profile. Cancel if dist < minDist
      if ( otherTransectWithinDistance( lineClipStratum, minDistanceLayerUnits, minDistance, sIndex, lineFeatureMap, distanceArea ) )
      {
        delete lineFarAwayGeom; delete lineClipStratum;
        continue;
      }

      QgsFeatureId fid( nCreatedTransects );
      QgsFeature sampleLineFeature( fid );
      sampleLineFeature.setGeometry( lineClipStratum );
      sampleLineFeature.setAttribute( "id", nTotalTransects + 1 );
      sampleLineFeature.setAttribute( "station_id", nCreatedTransects + 1 );
      sampleLineFeature.setAttribute( "stratum_id", strataId );
      sampleLineFeature.setAttribute( "station_code", strataId.toString() + "_" + QString::number( nCreatedTransects + 1 ) );
      sampleLineFeature.setAttribute( "start_lat", latLongSamplePoint.y() );
      sampleLineFeature.setAttribute( "start_long", latLongSamplePoint.x() );
      sampleLineFeature.setAttribute( "bearing", bearing );
      outputLineWriter.addFeature( sampleLineFeature );

      //add point to file writer here.
      //It can only be written if the corresponding transect has been as well
      outputPointWriter.addFeature( samplePointFeature );

      sIndex.insertFeature( sampleLineFeature );
      Q_NOWARN_DEPRECATED_PUSH
      lineFeatureMap.insert( fid, sampleLineFeature.geometryAndOwnership() );
      Q_NOWARN_DEPRECATED_POP

      delete lineFarAwayGeom;
      ++nTotalTransects;
      ++nCreatedTransects;
    }
    delete clippedBaseline;

    QgsFeature bufferClipFeature;
    bufferClipFeature.setGeometry( bufferLineClipped );
    bufferClipFeature.setAttribute( "id", strataId );
    bufferClipLineWriter.addFeature( bufferClipFeature );
    //delete bufferLineClipped;

    //delete all line geometries in spatial index
    QMap< QgsFeatureId, QgsGeometry* >::iterator featureMapIt = lineFeatureMap.begin();
    for ( ; featureMapIt != lineFeatureMap.end(); ++featureMapIt )
    {
      delete( featureMapIt.value() );
    }
    lineFeatureMap.clear();
    delete baselineGeom;

    ++nFeatures;
  }

  if ( pd )
  {
    pd->setValue( mStrataLayer->featureCount() );
  }

  return 0;
}
예제 #5
0
파일: speed.c 프로젝트: Noxbru/Rands
int main(int argc, const char *argv[])
{
    unsigned int i,j;
    unsigned long int elapsed_time;
    unsigned long int times[10];
    double mean, var;

    struct timespec tp1, tp2;

    printf("#======================================#\n");
    printf("|    Speed tests for the generators    |\n");
    printf("#======================================#\n");
    printf("#======================================#\n");
    printf("| The tests consists in measuring the  |\n");
    printf("| time it takes to each generator to   |\n");
    printf("| generate 10 million random numbers   |\n");
    printf("|                                      |\n");
    printf("+--------------------------------------+\n");
    printf("| 32 Bits                              |\n");
    printf("+--------------------------------------+\n");

#if ISAAC_RAND || RANDS_USE_ALL
    isaac_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            isaac_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| ISAAC:               %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if ISAAC_X64_RAND || RANDS_USE_ALL
    isaac_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 5000000; i++)
            isaac_x64_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| ISAAC (64bits):      %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if PR_RAND || RANDS_USE_ALL
    pr_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            pr_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Parisi-Rapuano:      %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if __SSE2__
#if PR_SSE_RAND || RANDS_USE_ALL
    pr_sse_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            pr_sse_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Parisi-Rapuano SSE:  %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif
#endif

    srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Rand (stdlib):       %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);

#if MT_RAND || RANDS_USE_ALL
    mt_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            mt_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Mersenne Twister:    %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if WELL_RAND || RANDS_USE_ALL
    well_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            well_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| WELL512:             %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if WELL_X64_RAND || RANDS_USE_ALL
    well_x64_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 5000000; i++)
            well_x64_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| WELL512 (64bits):    %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if XOR_RAND || RANDS_USE_ALL
    xor_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            xor_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Xorshift:            %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

    // END OF 32 BITS
    
    printf("+--------------------------------------+\n");
    printf("+--------------------------------------+\n");
    printf("| 64 Bits                              |\n");
    printf("+--------------------------------------+\n");

#if ISAAC_RAND || RANDS_USE_ALL
    isaac_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            isaac_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| ISAAC:               %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if ISAAC_X64_RAND || RANDS_USE_ALL
    isaac_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            isaac_x64_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| ISAAC (64bits):      %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if PR_RAND || RANDS_USE_ALL
    pr_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            pr_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Parisi-Rapuano:      %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if __SSE2__
#if PR_SSE_RAND || RANDS_USE_ALL
    pr_sse_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            pr_sse_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Parisi-Rapuano SSE:  %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif
#endif

    srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Rand (stdlib):       %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);

#if MT_RAND || RANDS_USE_ALL
    mt_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            mt_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Mersenne Twister:    %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if WELL_RAND || RANDS_USE_ALL
    well_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            well_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| WELL512:             %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if WELL_X64_RAND || RANDS_USE_ALL
    well_x64_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 10000000; i++)
            well_x64_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| WELL512 (64bits):    %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

#if XOR_RAND || RANDS_USE_ALL
    xor_srand(time(NULL));
    for(j = 0; j < 10; j++)
    {
        clock_gettime(CLOCK_MONOTONIC,&tp1);
        for(i = 0; i < 20000000; i++)
            xor_rand();
        clock_gettime(CLOCK_MONOTONIC,&tp2);
        elapsed_time = (unsigned long) (tp2.tv_sec-tp1.tv_sec)*1000000000 + \
                       (unsigned long) tp2.tv_nsec-tp1.tv_nsec;
        times[j] = elapsed_time;
    }
    calc_mean_var(&mean,&var,times);

    printf("| Xorshift:            %.4lf ± %.4lf |\n",mean/1e9, sqrt(var)/1e9);
#endif

    // END OF 64 BITS

    printf("+--------------------------------------+\n");
    
    return 0;
}
예제 #6
0
int main( int argc, char *argv[] )
{
	mt_srand(time(NULL));

	printf("\nWelcome to... >> %s %s <<\n\n", opentyrian_str, opentyrian_version);

	printf("Copyright (C) 2007-2009 The OpenTyrian Development Team\n\n");

	printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
	printf("This is free software, and you are welcome to redistribute it\n");
	printf("under certain conditions.  See the file GPL.txt for details.\n\n");

	if (SDL_Init(0))
	{
		printf("Failed to initialize SDL: %s\n", SDL_GetError());
		return -1;
	}

	JE_loadConfiguration();

	xmas = xmas_time();  // arg handler may override

	JE_paramCheck(argc, argv);

	JE_scanForEpisodes();

	init_video();
	init_keyboard();
	init_joysticks();
	printf("assuming mouse detected\n"); // SDL can't tell us if there isn't one

	if (xmas && (!dir_file_exists(data_dir(), "tyrianc.shp") || !dir_file_exists(data_dir(), "voicesc.snd")))
	{
		xmas = false;

		fprintf(stderr, "warning: Christmas is missing.\n");
	}

	JE_loadPals();
	JE_loadMainShapeTables(xmas ? "tyrianc.shp" : "tyrian.shp");

	if (xmas && !xmas_prompt())
	{
		xmas = false;

		free_main_shape_tables();
		JE_loadMainShapeTables("tyrian.shp");
	}


	/* Default Options */
	youAreCheating = false;
	smoothScroll = true;
	loadDestruct = false;

	if (!audio_disabled)
	{
		printf("initializing SDL audio...\n");

		init_audio();

		load_music();

		JE_loadSndFile("tyrian.snd", xmas ? "voicesc.snd" : "voices.snd");
	}
	else
	{
		printf("audio disabled\n");
	}

	if (record_demo)
		printf("demo recording enabled (input limited to keyboard)\n");

	JE_loadExtraShapes();  /*Editship*/

	JE_loadHelpText();
	/*debuginfo("Help text complete");*/

	if (isNetworkGame)
	{
#ifdef WITH_NETWORK
		if (network_init())
		{
			network_tyrian_halt(3, false);
		}
#else
		fprintf(stderr, "OpenTyrian was compiled without networking support.");
		JE_tyrianHalt(5);
#endif
	}

#ifdef NDEBUG
	if (!isNetworkGame)
		intro_logos();
#endif

	for (; ; )
	{
		JE_initPlayerData();
		JE_sortHighScores();

		if (JE_titleScreen(true))
			break;  // user quit from title screen

		if (loadDestruct)
		{
			JE_destructGame();
			loadDestruct = false;
		}
		else
		{
			JE_main();
		}
	}

	JE_tyrianHalt(0);

	return 0;
}
예제 #7
0
static void retro_init(void)
{
   mt_srand(time(NULL));
}