0

How can I generate a random number using Uniform distributed random number range between (Length of the string and 2000000), integer only., by using all the time constant seed(2) in random generation to get the same results in each run?

x = random.uniform(len(String),200)

How can I use seed next?

1
  • "by using all the time constant seed(2)": what ?? Commented Nov 14, 2022 at 12:41

2 Answers 2

4

You can use a list comprehension for a more compact (and potentially faster) code:

import random

# Fixed seed for repetitive results
const_seed = 200

# Bounds of numbers
n_min = 0
n_max = 2

# Final number of values 
n_numbers = 5

# Seed and retrieve the values
random.seed(const_seed)

numbers = [random.uniform(n_min, n_max) for i in range(0, n_numbers)]

print(numbers)

By always seeding with the same number your sequence of numbers will be the same (at least on the same platform - i.e. computer). Here is a confirmation from the official documentation.


This is the requested version that generates integers following a uniform distribution (the above creates floats). The smallest possible integer is the length of a string and the largest is 2,000,000:

import random

# Fixed seed for repetitive results
const_seed = 200

# Bounds of numbers
some_string = 'aString'
n_min = len(some_string)
n_max = 2000000

# Final number of values 
n_numbers = 5

# Seed and retrieve the values
random.seed(const_seed)

numbers = [random.randint(n_min, n_max) for i in range(0, n_numbers)]

print(numbers)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, and how can i change to set the range between (Length of the string and 2000000), integer only.
note that random.uniform gives you floats back, not integers. you'd want to use one of the other methods that gives ints back, e.g. randint or randrange. both are uniformly distributed
About to edit, thank you!
0

The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator. Note: If you use the same seed value twice you will get the same random number twice. using random.seed() and setting the same seed everytime will give the same output

here is an example:

import random
random.seed(12)
for i in range (1, 10):
    a = random.randint(1,10)
    print(a)

1 Comment

When you copy something from another site you should mention it w3schools.com/python/ref_random_seed.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.