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.
达州创梦网络怎么样,达州创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,一手四川托管服务商,成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快,直连省骨干,大网封UDP,无视UDP攻击,机房集群高达1.2TB,单机可提供1...
gcorelabs提供美国阿什本数据中心的GPU服务器(显卡服务器),默认给8路RTX2080Ti,服务器网卡支持2*10Gbps(ANX),CPU为双路Silver-4214(24核48线程),256G内存,1Gbps独享带宽仅需150欧元、10bps带宽仅需600欧元,不限流量随便跑吧。 官方网站 :https://gcorelabs.com/hosting/dedicated/gpu/ ...
RAKsmart怎么样?RAKsmart香港机房新增了付费的DDoS高防保护服务,香港服务器默认接入20Mbps的大陆优化带宽(电信走CN2、联通和移动走BGP)。高防服务器需要在下单页面的IP Addresses Option里面选择购买,分:40Gbps大陆优化高防IP-$461/月、100Gbps国际BGP高防IP-$692/月,有兴趣的可以根据自己的需求来选择!点击进入:RAKsmart官...