随机数java随机数生成(Java random number generation)

java随机数  时间:2021-02-13  阅读:()

java随机数生成Java random number generationMethod 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.

Bluehost美国虚拟主机2.95美元/月,十八周年庆年付赠送顶级域名和SSL证书

Bluehost怎么样,Bluehost好不好,Bluehost成立十八周年全场虚拟主机优惠促销活动开始,购买12个月赠送主流域名和SSL证书,Bluehost是老牌虚拟主机商家了,有需要虚拟主机的朋友赶紧入手吧,活动时间:美国MST时间7月6日中午12:00到8月13日晚上11:59。Bluehost成立于2003年,主营WordPress托管、虚拟主机、VPS主机、专用服务器业务。Blueho...

腾讯云2核4GB内存8M带宽 年74元

一般大厂都是通过首年才有可以享受爆款活动,然后吸引我们注册他们商家达到持续续费和购买的目的。一般只有大厂才能有这样的魄力和能力首年亏本,但是对于一般的公司和个人厂家确实难过,这几年确实看到不少的同类商家难以生存。这里我们可以看到有对应的套餐方案。不过这两个套餐都是100%CPU独享的,不是有某云商家限制CPU的。但是轻量服务器有个不好的就是带宽是较大且流量是限制的额,分别是1GB和1.2TB月流量...

AkkoCloud(60元/月 ),英国伦敦CN2 1核 768 MB 内存 10 GB SSD 硬盘 600GB 流量 英国伦敦CN2 1核  1.5G  300Mbps

官方网站:https://www.akkocloud.com/AkkoCloud新品英国伦敦CN2 GIA已上线三网回程CN2 GIA 国内速度优秀.电信去程CN2 GIALooking Glass:http://lonlg.akkocloud.com/Speedtest:http://lonlg.akkocloud.com/speedtest/新品上线刚好碰上国庆节 特此放上国庆专属九折循环优惠...

java随机数为你推荐
万维读者网罂粟花的含义?回收站在哪手机回收站在哪里打开百度k站百度K站是怎么回事?网络明星想知道3大网络歌手是谁?站长故事爱迪生发明电灯的故事简短在线漏洞检测漏洞扫描工具有哪些缓冲区溢出教程如何防止高手使用缓冲区溢出?bluestacks安卓模拟器BlueStacks如何安装使用?伪静态什么是伪静态伪静态有何作用9flashIE9flash模块异常。
com域名 美国服务器租用 免费国际域名 深圳域名空间 如何注销域名备案 过期域名抢注 win8.1企业版升级win10 tightvnc hnyd 华为4核 网通ip 灵动鬼影 40g硬盘 100m独享 网络空间租赁 服务器监测 联通网站 空间购买 路由跟踪 photobucket 更多