correct use of random seed in reading a text file

0 votes
Hi,

1. I am stuck in my program. i tried to set a random seed so i can get same values every time i run the program but i don't think i did it right as all my values in the program are now same...

2. I also want to read into a file and print the 10th char in that file but i don't know how to do so correctly. can someone show me how to pls?

Example, i set seed and randomly read into a text file and print the 10th character and keep printing characters until eof....

but when i printed the output, i noticed that all characters were same.
Jul 31, 2020 in Python by anonymous
• 120 points
1,819 views

1 answer to this question.

0 votes

Hi, @Flygen,

Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.

Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.

The random module uses the seed value as a base to generate a random number. if seed value is not present it takes system current time. if you provide the same seed value before generating random data it will produce the same data.

Suppose you generate 10 values, all these values won't be stored, the last value it generated will be stored because that value will work as the seed for the next number.

Use the above code to see a repeated output for yourself

import random

for i in range(2):
    random.seed(10)
    for i in range(20):
        print(random.random())
    print()
answered Aug 3, 2020 by Gitika
• 65,770 points
Thank you for explaining

Hey, 

Glad to help you, please upvote the answer if you find it helpful. You can come to the community regarding any tech queries to gain points and for further contributions. You may ask questions, answer, upvote, and downvote an answer. Each of these would fetch you points and you could be among the top contributors and win exciting merchandise from Edureka.

Cheers!

Related Questions In Python

0 votes
0 answers
0 votes
0 answers

Storing a list of arrays into a CSV file and retrieving it back in a different program

This is the code that I am ...READ MORE

Jun 7, 2018 in Python by aryya
• 7,460 points
2,397 views
+1 vote
1 answer

Reading a large file, line by line in Python

The correct, fully Pythonic way to read ...READ MORE

answered Aug 21, 2018 in Python by Priyaj
• 58,020 points
1,074 views
0 votes
1 answer

Need help extracting a schema to make use for an avro file in Python

Hi, nice question. So what I daily use ...READ MORE

answered Jan 10, 2019 in Python by Nymeria
• 3,560 points
5,007 views
0 votes
0 answers

how can i read a text file in python?

can you specify the syntax and prequisites ...READ MORE

Apr 4, 2019 in Python by Waseem
• 4,540 points
605 views
0 votes
1 answer

How to find the value of a row in a csv file in python?

If you want to find the value ...READ MORE

answered May 20, 2019 in Python by Sanam
19,260 views
0 votes
0 answers

How can we read a text file in a list in python?

Can you give the sample code as ...READ MORE

Jun 18, 2019 in Python by Waseem
• 4,540 points
560 views
+7 votes
8 answers

What exactly is the function of random.seed() in python?

The seed method is used to initialize the ...READ MORE

answered Oct 29, 2018 in Python by Rahul
126,939 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP