随机数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.

青云互联:香港安畅CN2弹性云限时首月五折,15元/月起,可选Windows/可自定义配置

青云互联怎么样?青云互联是一家成立于2020年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、韩国服务器、香港服务器、美国云服务器,香港安畅cn2弹性云限时首月五折,15元/月起;可选Windows/可自定义配置,让您的网站高速、稳定运行。点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联香港云服务器活动...

搬瓦工:新增荷兰机房 EUNL_9 测评,联通 AS10099/AS9929 高端优化路线/速度 延迟 路由 丢包测试

搬瓦工最近上线了一个新的荷兰机房,荷兰 EUNL_9 机房,这个 9 的编号感觉也挺随性的,之前的荷兰机房编号是 EUNL_3。这次荷兰新机房 EUNL_9 采用联通 AS9929 高端路线,三网都接入了 AS9929,对于联通用户来说是个好消息,又多了一个选择。对于其他用户可能还是 CN2 GIA 机房更合适一些。其实对于联通用户,这个荷兰机房也是比较远的,相比之下日本软银 JPOS_1 机房可...

DiyVM(50元起)老牌商家,香港沙田CN2直连vps/不限流量/五折终身优惠

diyvm怎么样?diyvm是一家国内成立时间比较久的主机商家了,大约在6年前站长曾经用过他家的美国机房的套餐,非常稳定,适合做站,目前商家正在针对香港沙田机房的VPS进行促销,给的是五折优惠,续费同价,香港沙田机房走的是CN2直连的线路,到大陆地区的速度非常好,DiyVM商家采用小带宽不限流量的形式,带宽2Mbps起步,做站完全够用,有需要的朋友可以入手。diyvm优惠码:五折优惠码:OFF50...

java随机数为你推荐
郭吉军郭吉军和管鹏这两个站长怎么样?群里有人骂淘宝收费淘宝都什么服务是收费的?人人时光机怎么查看人人网的注册时间?什么是电子邮件 什么是电子邮件万网核心代理哪里可以注册免费代理?中国论坛大全中国十大网站是?微信如何建群微信可以建立两个人的群吗?有一个是自己照片转手绘美图秀秀可以照片转手绘吗?是手机版的今日热点怎么删除千牛里面的今日热点怎么取消_?申请证书申请毕业证书
美国虚拟主机购买 北京域名空间 国内vps vps优惠码cnyvps 申请免费域名 80vps 分销主机 pw域名 香港新世界电讯 阿里云代金券 2017年万圣节 个人免费空间 湖南服务器托管 33456 域名与空间 备案空间 服务器维护 注册阿里云邮箱 成都主机托管 空间申请 更多