Math.random() | [0 .................................... 1) [0 .................................... max - min) | x (what we need)
// 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 });
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 ); }
private float randomize (float x) { return x + (float)Math.random() * amplitude * 2 - 1; } }
private float randomize (float x) { return x + (float)Math.random() * amplitude * 2 - 1; } }
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; }
var item = items[Math.floor(Math.random()*items.length)];
var rand = myArray[Math.floor(Math.random() * myArray.length)];
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; }
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()); }
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; }
public Caveman (float x, float y, boolean headsLeft) { pos = new Vector2().set(x, y); this.headsLeft = headsLeft; this.stateTime = (float)Math.random(); }
@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)); } } }
@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
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(); })
@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
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); }
@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)))); }
@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 }; }