Java Random Number Generation

Source: Internet
Author: User

// Generate 50 random numbers between 0 and 500 ([0500)

 

Int number = 0;

For (INT I = 0; I <50; I ++)
{
Number = (INT) (math. Random () * 500 );
This. Numbers. insert (number); // insert in empty list
}

 

Each operation can generate 50 random numbers (not sure if there may be duplicates ),

In addition, the running result is different every two times, and you do not need to set a seed like C ++.

 

The following is an encapsulation that is easier to use.Code: Private Static int getrandomvalue (INT lowerbound, int upperbound) {int retval = lowerbound; If (lowerbound! = Upperbound) {If (lowerbound> upperbound) {int TMP = lowerbound; lowerbound = upperbound; upperbound = TMP;} retval = lowerbound + (INT) (math. random () * (upperbound-lowerbound + 1);} return retval ;}

Reference: http://zhidao.baidu.com/question/93760119.html? Fr = ala0

More complex code can be referred to: http://www.cnblogs.com/Fskjb/archive/2009/08/29/1556417.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.