java随机数生成Java random number generationMethod of generating random numbers by Java
First, the random method is used to generate random numbers.Generating random numbers in Java language is relatively simple,because there is a ready-made method that can be used. In theMath class, the Java language provides a method called random.This method allows the system to generate random numbers.However, by default, the range of random numbers generated bythem is relatively small, which is double random numbersgreater than or less than 0 to 1. Although its random numberrange is relatively small, it can not meet the daily needs.Random numbers that may be generated in daily work. In fact,as long as the method is handled flexibly, the random numberin any range can be obtained.
If we can first generate a random number by the random method,and then multiply the result by 10. The randomnumbers generatedat this time are greater than or equal to 0 less than 10 of thenumber. And then use the Int method to convert (it will removethe decimal off the number, that is, only the integer part, notfour into five) . Finally, we can obtain an integer random numberfrom 0 to 9. The implementation method is very simple, that is,the original random method is changed according to thefollowing format: (int) (Math.Random () *10) . In fact, we canextend this method to produce random numbers in arbitraryranges. It is necessary to change this 10 to N, such as (int)(Math.Random ()) *n. At this point, the application generatesa random number that is larger than or equal to 0 small and n.If you set n to 5, then it generates an integer random number
between 0 and 5. If this is written as a parametric method, thenthe method can be used to generate the randomnumber in the rangeas long as the user input needs to generate the maximum numberof random numbers. Defining your own library of tools in JavaSometimes a programmer may need to generate a random number orodd number within a specified range. Is it possible to do itby this means? The answer is yes. If the program needs togenerate an even number within the range of 1-100. How do youdo this?First of all, you need to generate a random number from0 to 99 (as for why here is 99) , you' ll be patient and you' llknow why. To implement this requirement, it is very simple, aslong as the following statement can be implemented: i=1+ (int)(Math.Random () *100) . Where (i nt) (Math.Random () *99)generates integer random numbers from 0 to 99. Then plus 1 isa random integer that generates 1 to 100. Then the resultingrandom number is assigned to the variable I. But the randomnumbers generated at that time are even numbers and odd numbers.And now what programmers need is a random even number. Then wecan add a if judgment sentence at the back. Divide this randomnumber by 2, and if there is no remainder (or the remainder is
0) , it means that the random number is even and can be returneddirectly. If the remainder of the return is not zero, then itis an odd number. If we add 1, we change to even number and return.Note, in the above random number generation, the author usesthe range of 0 to 99, and then add 1 to let it change to 1 to100 random number. The final result is a random number between1 and 100. In fact, if the scope of the random odd number, tothe above statement needs to be slightly modified.
===============================
Suppose the user wants to generate an odd number or even numberin any range, can it be implemented? Suppose the user wants toimplement any even number between M and n (M
It can be seen that although the random method itself producesrandom numbers, there is a strict range limit. However, as longas it is reasonably transformed, programmers can still use thismethod to generate random data needed by users.
Two, generate random numbers through the Random class.In Java, apart from generating random numbers through therandom method,
You can also generate random numbers through a random class.Program developers can create a random number generator byinstantiating a Random object. Such as Random i=new Random () .Through this statement, the Random class is used to create arandom number generator. However, when creating random numbersin this way, the mechanism is different from that of usingRandom method to generate random numbers. When you instantiatethe object in this way, the Java compiler takes the current timeof the system as the seed of the random number generator.Because time changes all the time. If this time is used as theseed of the generator, it can guarantee that the generatedrandom number is really random, and the repetition rate of therandom number generated by it will be greatly reduced.It is convenient to use this method. If you can use the keywordprovided, let the program return to a random integer (using int
nextInt (10)) and so on. However, its return control is a littleharder than the Random method. If you need a system to providea random odd number between 10 and 50, you can't do it with thisRandom class. That is to say, by using this Random class togenerate random numbers, it can only control the upper limit,but can not control the lower bound. In other words, it canspecify the maximum range of random numbers and not specify theminimum range of random numbers. So, in flexibility, it' s alittle bit worse than the Random approach.
In addition, you must create an object by using this method.That is, using the Randow class to create objects. This isdifferent from the Randow method. As in the example above, theRandow method itself is a method in the math class, which canbe called directly, eliminating the method of object creation.For this reason, the author suggests that readers and programdevelopers should use the Random method to create randomnumbers. The Random class is used only when some special randomnumbers are generated. If it is necessary to generate a doubleprecision random number with probability density as Gaussdistribution, it is relatively simple to create a random numberby using the Random class method.
Three, generate random characters.
The two methods described above generate random numeric data.But sometimes the user may also need to generate randomcharacters. In fact, random can also be used to generate randomcharacters. If you can use code to generate a random lower casecharacter: (char) (a '+Math.random () * (' Z '-' a '+1)) . Infact, this is similar to the random number that generates any
two numbers. Through the above code, you can generate arbitraryrandom characters within a range. By properly trimming the code,you can also generate random characters between any twocharacters and any random characters with uppercase characters.The transformation is similar to the random number in any rangementioned above. Readers, if you're interested, you can testit yourself. The teacher leads the door and practices himself.If the author here altogether will all tell you the answer, theimpression is not deep. If you go back and try it yourself, it' seasier to remember.
The author here gives you a hint, only need to according to m+(int) (Math.Random () * (n-m) this statement to adjust (char)(a '+Math.random () * (' Z '-' a '+1)) this code can be.Finally, the author needs to emphasize that when the user entersthe parameters, it is better to check the parameters importedby the user. In order to avoid the user' s incoming parametersdo not meet the established requirements, such as theparameters need to be numerical data, and the introduction ofthe data is character type, and lead to the program runningerror.
有一段时间没有分享Gcore(gcorelabs)的信息了,这是一家成立于2011年的国外主机商,总部位于卢森堡,主要提供VPS主机和独立服务器租用等,数据中心包括俄罗斯、美国、日本、韩国、新加坡、荷兰、中国(香港)等多个国家和地区的十几个机房,商家针对不同系列的产品分为不同管理系统,比如VPS(Hosting)、Cloud等都是独立的用户中心体系,部落分享的主要是商家的Hosting(Virtu...
易探云怎么样?易探云最早是主攻香港云服务器的品牌商家,由于之前香港云服务器性价比高、稳定性不错获得了不少用户的支持。易探云推出大量香港云服务器,采用BGP、CN2线路,机房有香港九龙、香港新界、香港沙田、香港葵湾等,香港1核1G低至18元/月,183.60元/年,老站长建站推荐香港2核4G5M+10G数据盘仅799元/年,性价比超强,关键是延迟全球为50ms左右,适合国内境外外贸行业网站等,如果需...
欧路云新上了美国洛杉矶cera机房的云服务器,具备弹性云特征(可自定义需要的资源配置:E5-2660 V3、内存、硬盘、流量、带宽),直连网络(联通CUVIP线路),KVM虚拟,自带一个IP,支持购买多个IP,10G的DDoS防御。付款方式:PayPal、支付宝、微信、数字货币(BTC USDT LTC ETH)测试IP:23.224.49.126云服务器 全场8折 优惠码:zhujiceping...