- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python random.seed() Method
The Python random.seed() method is used to generate the random numbers. It is done with the help of pseudo-random number generator by performing some operation on the given value. The random module creates a random number in Python using the seed value as a base.
A pseudo-random number generator obtains its first "previous" value during seeding. For a specific random number generator, each seed value will correspond to a series of values that were generated. In other words, if you use the same seed twice, you will obtain the same set of numbers twice.
Note − This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.
Syntax
Following is the syntax of Python random.seed() method −
random.seed(x, version)
Parameters
x (optional)− This is the seed for the next random number. If omitted, then it takes system time to generate next random number. If the x is None, then by default the current system time is used.
version− This is an integer value. It depicts how to convert the parameter x into an integer value. It's default value is 2.
Return Value
This method does not return any value. It only fixes the random number generation.
Example
The following example shows the usage of the Python random.seed() method. Here, we also used another method called random() of the random module. It generates the random number lying between the specified range '10'.
import random random.seed( 10 ) print ("Random number with seed 10 : ", random.random()) # It will generate same random number random.seed(10) print ("Random number with seed 10 : ", random.random()) # It will generate same random number random.seed(10) print ("Random number with seed 10 : ", random.random())
When we run above program, it produces following result −
Random number with seed 10 : 0.57140259469 Random number with seed 10 : 0.57140259469 Random number with seed 10 : 0.57140259469
Example
If we use the same see twice,we will obtain the same set of random numbers twice.
In the example give below the same seed value '98' is provided to the random.seed() method. Hence, the same random number is obtained. This is because the first previous value (seed) is specified for the random number generator. Therefore, in each run, the generator will generate the same number.
# import the random module import random # set the seed with the value 98 x = random.seed(98) # generate the random number between 1-100 print("The random number generated is:", random.random()*100) # again set the seed with the value of 98 x = random.seed(98) # generate the random number between 1-100 print("The random number generated is:", random.random()*100)
While executing the above code we get the following output −
The random number generated is: 35.639820676272215 The random number generated is: 35.639820676272215
Example
In here, we are passing different seed values before calling the random module. Therefore, we obtain different random numbers.
# import the random module import random # set the seed with the value 98 x = random.seed(30) # generate the random number between 1-100 print("The random number generated is:", random.random()*100) # again set the seed with the value of 60 x = random.seed(60) # generate the random number print("The random number generated is:", random.random())
Following is an output of the above code −
The random number generated is: 53.90815646058106 The random number generated is: 0.3078067547337774
Example
Following is another example of seed() method using for loop. In here, we see the repeated output 4 times.
import random for i in range(4): # random number with seed 9 random.seed(9) for i in range(3): print('The random number with seed 9 is:',random.random()) print('The random number generated is:')
Output of the above code is as follows −
The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is:
To Continue Learning Please Login
Login with Google