Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make simple four-digit CAPTCHA code for Python random module

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you how to make a simple four-digit CAPTCHA on the Python random module. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Let's first introduce the random module in python.

Comparison of random and numpy.random:

1. Random.random (): generate random floating point numbers between [0rem 1]

Numpy.random.random (): generate random floating-point numbers between [0Phon1)

Numpy.random.random (size= (2)), produces a 2-dimensional random array with 2 random numbers per dimension, data interval [0J1]

2. Random.randint (aformab): produce random integers between [aformab]

Numpy.random.random (1mem5): returns an one-dimensional array of 5 elements with a data interval of [1mem5)

Numpy.random.random: returns a two-dimensional array of 2 random numbers for each dimension, with a data interval of [1d5].

3. Random.randrange: return random numbers in [1mem3, 5, 5, 7, 9].

4. Random.choice (seq): returns a random element of a sequence from a non-empty sequence

Numpy.random.choice (5Power2) randomly generates an array of two random integers from [0Power5).

5. Random.uniform (aformab): randomly produce floating-point numbers x, [aPermib]

6. Random.randn (2): returns a 2-dimensional random array with 2 random numbers for each dimension

The returned value of random.rand () conforms to the standard normal distribution, and the data interval is [0jue 1].

The following is an example code of the four-digit CAPTCHA function of the Python random module.

The code is as follows:

Import random # calling the random module checkcode='' # is equivalent to creating an empty string called checkcode for i in range (4): # the next operation loops four times (finally generating a 4-digit CAPTCHA) current=random.randrange (0Query 4) # randomly select 0 1 if current==i: # if the generated random number is exactly equal to the current digit bit tmp=chr (random.randint (65J 90)), then an integer of 65 Meli 90 is randomly generated and converted to the character (chr (I) in the ASCII code, which means: I can be a decimal or hexadecimal number, and the return value is the ASCII character corresponding to the current integer. ) 'else: # if the generated random number is not equal to the current digit bit tmp=random.randint (0 9) # return an integer checkcode+=str (tmp)' checkcode=checkcode+str (tmp) of 0 Murmur9, convert each generated number or letter into a string called checkcode and put it in a string called 'print (checkcode) # print out the checkcode list

The above is the editor for you to share the Python random module how to make a simple four-digit CAPTCHA, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report