Tabnine Logo
Math.random
Code IndexAdd Tabnine to your IDE (free)

How to use
random
method
in
java.lang.Math

Best Java code snippets using java.lang.Math.random (Showing top 20 results out of 19,899)

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 };
}
java.langMathrandom

Javadoc

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.

Popular methods of Math

  • min
    Returns the smaller of two long values. That is, the result is the argument closer to the value of L
  • max
    Returns the greater of two long values. That is, the result is the argument closer to the value of L
  • abs
    Returns the absolute value of a long value. If the argument is not negative, the argument is returne
  • round
    Returns the closest int to the argument, with ties rounding up. Special cases: * If the argument is
  • pow
    Returns the value of the first argument raised to the power of the second argument. Special cases: *
  • sqrt
    Returns the correctly rounded positive square root of a double value. Special cases: * If the argume
  • ceil
    Returns the smallest (closest to negative infinity) double value that is greater than or equal to th
  • floor
    Returns the largest (closest to positive infinity) double value that is less than or equal to the ar
  • sin
    Returns the trigonometric sine of an angle. Special cases: * If the argument is NaN or an infinit
  • cos
    Returns the trigonometric cosine of an angle. Special cases: * If the argument is NaN or an infin
  • log
    Returns the natural logarithm (base e) of a doublevalue. Special cases: * If the argument is NaN
  • exp
    Returns Euler's number e raised to the power of a double value. Special cases: * If the argument
  • log,
  • exp,
  • toRadians,
  • atan2,
  • log10,
  • acos,
  • tan,
  • toDegrees,
  • atan

Popular in Java

  • Start an intent from android
  • scheduleAtFixedRate (Timer)
  • setScale (BigDecimal)
  • notifyDataSetChanged (ArrayAdapter)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • FileReader (java.io)
    A specialized Reader that reads from a file in the file system. All read requests made by calling me
  • SQLException (java.sql)
    An exception that indicates a failed JDBC operation. It provides the following information about pro
  • Collection (java.util)
    Collection is the root of the collection hierarchy. It defines operations on data collections and t
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • JLabel (javax.swing)
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now