java.lang.Math.random()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(210)

本文整理了Java中java.lang.Math.random()方法的一些代码示例,展示了Math.random()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.random()方法的具体详情如下:
包路径:java.lang.Math
类名称:Math
方法名:random

Math.random介绍

[英]Returns a pseudo-random double n, where n >= 0.0 && n < 1.0. This method reuses a single instance of java.util.Random. This method is thread-safe because access to the Random is synchronized, but this harms scalability. Applications may find a performance benefit from allocating a Random for each of their threads.
[中]返回伪随机双精度n,其中n>=0.0&&n<1.0。此方法重用单个java实例。util。随机的这种方法是线程安全的,因为对随机数据的访问是同步的,但这会损害可伸缩性。应用程序可能会发现,为每个线程分配一个随机线程会提高性能。

代码示例

代码示例来源:origin: stackoverflow.com

Math.random()
          |
[0 .................................... 1)
[0 .................................... max - min)
          |
          x (what we need)

代码示例来源:origin: stackoverflow.com

// get distribution
var counts = [], rand, len;
for (var i=0; i<1000000; i++) {
 rand = Math.random();
 len  = String(rand).length;
 if (counts[len] === undefined) counts[len] = 0;
 counts[len] += 1;
}

// calculate % frequency
var freq = counts.map(function(n) { return n/1000000 *100 });

代码示例来源:origin: stackoverflow.com

if (_ok == true) {
 _logger.log( Level.WARNING , "Server seen down: " + _addr, e );
} else if (Math.random() < 0.1) {
 _logger.log( Level.WARNING , "Server seen down: " + _addr );
}

代码示例来源:origin: libgdx/libgdx

private float randomize (float x) {
    return x + (float)Math.random() * amplitude * 2 - 1;
  }
}

代码示例来源:origin: libgdx/libgdx

private float randomize (float x) {
    return x + (float)Math.random() * amplitude * 2 - 1;
  }
}

代码示例来源:origin: stackoverflow.com

function makeid()
{
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for( var i=0; i < 5; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

代码示例来源:origin: stackoverflow.com

double random = Math.random() * 50 + 1;
or
int random = (int )(Math.random() * 50 + 1);

代码示例来源:origin: stackoverflow.com

var item = items[Math.floor(Math.random()*items.length)];

代码示例来源:origin: stackoverflow.com

var rand = myArray[Math.floor(Math.random() * myArray.length)];

代码示例来源:origin: shuzheng/zheng

private String randomStr(int n) {
  String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  String str2 = "";
  int len = str1.length() - 1;
  double r;
  for (int i = 0; i < n; i++) {
    r = (Math.random()) * len;
    str2 = str2 + str1.charAt((int) r);
  }
  return str2;
}

代码示例来源:origin: bumptech/glide

private static Key getFrameSignature() {
 // Some devices seem to have crypto bugs that throw exceptions when you create a new UUID.
 // See #1510.
 return new ObjectKey(Math.random());
}

代码示例来源:origin: libgdx/libgdx

public BulletEntity shoot (final String what, final float x, final float y, final float impulse) {
  // Shoot a box
  Ray ray = camera.getPickRay(x, y);
  BulletEntity entity = world.add(what, ray.origin.x, ray.origin.y, ray.origin.z);
  entity.setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
    1f);
  ((btRigidBody)entity.body).applyCentralImpulse(ray.direction.scl(impulse));
  return entity;
}

代码示例来源:origin: libgdx/libgdx

public Caveman (float x, float y, boolean headsLeft) {
  pos = new Vector2().set(x, y);
  this.headsLeft = headsLeft;
  this.stateTime = (float)Math.random();
}

代码示例来源:origin: libgdx/libgdx

@Override
public void create () {
  super.create();
  // Create the entities
  world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(),
    0.25f + 0.5f * (float)Math.random(), 1f);
  for (float x = -5f; x <= 5f; x += 2f) {
    for (float y = 5f; y <= 15f; y += 2f) {
      world.add("box", x + 0.1f * MathUtils.random(), y + 0.1f * MathUtils.random(), 0.1f * MathUtils.random()).body
        .setUserValue((int)((x + 5f) / 2f + .5f));
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void subscribe(Subscriber<? super Integer> s) {
    BooleanSubscription bs = new BooleanSubscription();
    s.onSubscribe(bs);
    while (!bs.isCancelled()) {
      // burst some number of items
      for (int i = 0; i < Math.random() * 20; i++) {
        s.onNext(i);
      }
      try {
        // sleep for a random amount of time
        // NOTE: Only using Thread.sleep here as an artificial demo.
        Thread.sleep((long) (Math.random() * 200));
      } catch (Exception e) {
        // do nothing
      }
    }
    System.out.println("Hot done.");
  }
}).subscribeOn(Schedulers.newThread()); // use newThread since we are using sleep to block

代码示例来源:origin: stackoverflow.com

function guid() {
 return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  s4() + '-' + s4() + s4() + s4();
}

function s4() {
 return Math.floor((1 + Math.random()) * 0x10000)
  .toString(16)
  .substring(1);
}

document.getElementById('jsGenId').addEventListener('click', function() {
 document.getElementById('jsIdResult').value = guid();
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void subscribe(Observer<? super Integer> observer) {
    Disposable d = Disposables.empty();
    observer.onSubscribe(d);
    while (!d.isDisposed()) {
      // burst some number of items
      for (int i = 0; i < Math.random() * 20; i++) {
        observer.onNext(i);
      }
      try {
        // sleep for a random amount of time
        // NOTE: Only using Thread.sleep here as an artificial demo.
        Thread.sleep((long) (Math.random() * 200));
      } catch (Exception e) {
        // do nothing
      }
    }
    System.out.println("Hot done.");
  }
}).subscribeOn(Schedulers.newThread()); // use newThread since we are using sleep to block

代码示例来源:origin: libgdx/libgdx

private void launch () {
  m_body.setTransform(new Vector2(0, 20), 0);
  m_angularVelocity = (float)Math.random() * 100 - 50;
  m_body.setLinearVelocity(new Vector2(0, -100));
  m_body.setAngularVelocity(m_angularVelocity);
}

代码示例来源:origin: libgdx/libgdx

@Override
public void create () {
  texture = new Texture(Gdx.files.internal("data/badlogicsmall.jpg"));
  stage = new Stage();
  for (int i = 0; i < 100; i++) {
    Image img = new Image(new TextureRegion(texture));
    img.setX((float)Math.random() * 480);
    img.setY((float)Math.random() * 320);
    img.getColor().a = (float)Math.random() * 0.5f + 0.5f;
    stage.addActor(img);
  }
  stage.getRoot().addAction(forever(sequence(fadeOut(3), fadeIn(3))));
}

代码示例来源:origin: bumptech/glide

@Before
public void setUp() {
 dir = RuntimeEnvironment.application.getCacheDir();
 cache = DiskLruCacheWrapper.create(dir, 10 * 1024 * 1024);
 key = new ObjectKey("test" + Math.random());
 data = new byte[] { 1, 2, 3, 4, 5, 6 };
}

相关文章