Min menu

Pages

random model functions in Python

Python numbers  functionrandom()

its definition

Returns a random decimal between 0.0 and 1.0 .

So, the range of numbers you return is: 0.0 <= N < 1.0.


built

                  random()
	  

parameters

Do not accept any parameters.


Return value

Returns a decimal value between 0.0 and 1.0 .


first example

Test.py
                    # random() which contains the random function here we called the module
		  import random
	  
		  # random() Here we have shown the number returned by the function
		  print('Random Number = ', random.random())
	  
		  # random() Here we have shown the number returned by the function
		  print('Random Number = ', random.random())
	  
		  # random() Here we have shown the number returned by the function
		  print('Random Number = ', random.random())
	

We will get three different values ​​between 0.0 and 1.0 when running.

Random Number = 0.8630498170020632
Random Number = 0.73163813313323908
Random Number = 0.9113637080594214

note

We can change the range of numbers that the function can return using arithmetic operations.
For example, simply, we can multiply the number returned by the function by 10 and convert the result to intbecome the range between 0 =< N < 10.

Note

 We can change the range of numbers the function can return using math. For example, we can simply multiply the number returned by the function by 10 and convert the result to int, so that the range is between 0 = <N <10.

second example

Test.py
                    # random() which contains the random function here we called the module
		  import random
	  
		  # Then we display int with 10 and then convert the result to random() here we multiply the number returned by the function
		  print('Random Number = ', int(random.random()*10))
	  
		  # Then we display int with 10 and then convert the result to random() here we multiply the number returned by the function
		  print('Random Number = ', int(random.random()*10))
	  
		  # Then we display int with 10 and then convert the result to random() here we multiply the number returned by the function
		  print('Random Number = ', int(random.random()*10))
	

We will get three different values ​​between 0.0 and 1.0 when running.

Random Number = 8
Random Number = 5
Random Number = 1

Python numbers  functionuniform()

its definition

Returns a random decimal between the value we pass in the place of the parameter aand the value we pass in the place of the parameter b.

So, the range of numbers you return is: a <= N <= b.

Python numbers uniform function () Define it Returns a random number between the value we pass in the place of the parameter a and the value we pass to it in the place of the parameter b. So, the range of numbers you return is: a <= N <= b.

built

                  uniform( a, b )
	  

parameters

  • aA decimal number that specifies the lowest value that the function can generate.

  • bA float that specifies the maximum value that the function cannot return.


Return value

Returns a random integer between the value we pass in has the parameter's place aand the value we pass has the parameter's place b.


Example

Test.py
                    # uniform() which contains the random function here we called the module
		  import random
	  
		  # Here we have printed three random numbers between 1 and 10
		  print('Random Number between [1-10] =', random.uniform(1, 10))
		  print('Random Number between [1-10] =', random.uniform(1, 10))
		  print('Random Number between [1-10] =', random.uniform(1, 10))
	

We will get three different values ​​within three different ranges when running.

Random Number between [
1-10
] = 7.918523869665445

Python numbers  functionrandrange()

its definition

Returns a random integer between 0 and the value we pass to have the parameter's place stop.
Or it returns a random integer between the value we pass has the place of the parameter startand the value we pass has the place of the parameter stop.

This function can be called in three ways, which we will learn about later in the example.


built

                  ranrange( [start,] stop [,step] )
	  

parameters

  • startAn integer that specifies the lowest value that the function can generate.
    If the parameter's place value is not passed, it stopwill be considered 0 .

  • stopAn integer specifying the maximum value that the function cannot return.

  • stepAn integer specifying how much the returned random value will be multiplied, taking into account that the output must be within the specified range.


Return value

Returns a random integer between 0 and the value we pass to have the parameter's place stop.
Or it returns a random integer between the value we pass has the place of the parameter startand the value we pass has the place of the parameter stop.


Example

Test.py
                    # randrange() which contains the random function here we called the module
		  import random
	  
		  # Here we have printed a random number between 0 and 10
		  print('Random Number between [0-10] = ', random.randrange(10))
	  
		  # Here we have printed a random number between 1 and 50
		  print('Random Number between [1-50] = ', random.randrange(1, 50))
	  
		  # Here we have printed a random number between 1 and 50 with the return value multiplied by 5
		  print('Random Number between [1-50, step=5] = ', random.randrange(1, 50, 5))
	

We will get three different values ​​within three different ranges when running.

Random Number between [0-10] = 4
Random Number between [1-50] = 20
Random Number between [1-50, step=5] = 35

Python numbers  functionchoice()

its definition

Returns a random value from among the elements of any string(Sequence)We pass it on to her.

The string can be an array of numbers, or plain text (ie a string of characters).
For example, if we pass it an array of numbers, it returns a random number from within this array. And if we pass a text to it, it returns a random character from within that text.

Python numbers function choice () Define it returns a random value from among the elements of any sequence we pass to it. A string can be an array of numbers, or plain text (meaning a string of characters). For example, if we pass an array of numbers to it, it returns a random number within that matrix. And if we pass it to a text, it returns a random character from within that text.

built

                  choice( seq )
	  

parameters

seqAn array or text.


Return value

  • If we pass it an array of numbers, it returns a random number from within that array.

  • If we pass a text to it, it returns a random character from within that text.


Example

Test.py
                    # choice() which contains the random function Here we called the module
		  import random
	  
		  # contains text s Here we have defined a variable named
		  s = 'harmash'
	  
		  # lst Here we have defined an array containing numbers whose name is
		  lst = [1, 2, 3, 4, 9]
	  
		  # lst Here we have printed the value of a random element of the array
		  print('Random element from the list: ', random.choice(lst))
	  
		  # s Here we have printed a random character from the characters of the variable
		  print('Random character from the string:', random.choice(s))
	

We will get the following result when running.

Random element from the list: 4
Random character from the string: r

Python numbers  functionshuffle()

its definition

We pass an array of the parameter 's position xto it, and it randomly switches the positions of its elements.


built

                  shuffle( x )
	  

parameters

xAn array that has no specified type.


Return value

It does not return a value.


Example

Test.py
                    import random # shuffle() which contains the random function Here we called the module
	  
		  arr = [1, 2, 3, 4, 5] # arr Here we have defined an array containing numbers whose name is
	  
		  print('arr =', arr) # arr Here we have printed the values ​​of all the elements of the array
	  
		  random.shuffle(arr) # shuffle() randomly by the arr function here we shuffle the elements of the array
	  
		  print('arr =', arr) # Again to see the difference arr Here we have printed the values ​​of all the elements of the array
	

We will get the following result when running.

arr = [1, 2, 3, 4, 5]
arr = [3, 1, 4, 5, 2]