Hướng dẫn lấy số ngẫu nhiên trong lập trình android

Hướng dẫn lấy số ngẫu nhiên trong lập trình android

Muốn lấy ra một giá trị ngẫu nhiên, ta có 2 cách sau đây

Cách 1: Tạo số ngẫu nhiên từ a->b

Min+(int)(Math.random()*((Max-Min)+1)) 

Vd: Lấy ngẫu nhiên từ 2->5

2+(int)(Math.random()*(5-2)+1)) 

Cách 2:

Random rd=new Random();
x=rd.nextInt((Max-Min+1)+Min); 

Vd:Lấy ngẫu nhiên 2->5

Random rd=new Random();
x=rd.nextInt((5-2+1)+2);

Bài Tập

  • Tạo ngẫu nhiên 1 bài toán cộng, và làm ẩn đi một phần tử ngẫu nhiên, yêu cầu người dùng nhập vào số đó.
  • Click nút chơi lại sẽ load bài toán mới.

Hướng dẫn lấy số ngẫu nhiên trong lập trình android

File: 
Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 votes)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Một số điểm quan trọng khi sử dụng Drupal 8 Composer lưu ý

Một số điểm quan trọng khi sử dụng Drupal 8 Composer lưu ý

Whether you are familiar with Composer or not, using it to manage dependencies on a Drupal project entails its own unique set of best practices. In this article, we will start by getting a Composer-managed Drupal project set up

Giới thiệu Handling Uncertainty khi Estimating Software Projects

Giới thiệu Handling Uncertainty khi Estimating Software Projects

As before, we’re using Wideband Delphi. You’ll need two (or more) people to estimate

Domain Authority là gì?

Domain Authority là gì?

Domain Authority (DA) là một thước đo của SEOmoz dùng để dự đoán “sức mạnh” của website này so với website khác trên bảng xếp hạng của các SE.

Công ty diệt chuột T&C

 

Diet con trung