+ 3
how to only using seed once for the entire program ?
so i did a test https://code.sololearn.com/ckbqiyArECcg/?ref=app is there any efficent way other than calling seed each time i need it ? i have several random number that i need to get based on the seed given. something like what we can toggle ourself, from using and not using seed.
13 Antworten
+ 1
its soo messy like really messy, but the flow goes like this.
i have some lists
listA
listB
listC
then i generate a seed, by using randint
xseed = randint(100,1000)
then i shuffle the list using that seed.
what i looking for is to only use seed() once but will affect all the shuffle operation onwards.
my current code looks like
seed(xseed)
shuffle(listA)
#dosomething else
seed(xseed)
shuffle(listB)
#dosomething else
...
i looking for a solution like this
seed_once(xseed)
shuffle(listA)
#dosomething else
shuffle(listB) #but still affected from previous seed
#dosomething else
...
reset_seed() #to put back the normal randomizer
as why i need it is, seed() is pretty small line, when working on large source code i can easily miss them and mess with the result.
0
Can you eplain what you want to do exactly ?
0
as in my code
i want the output looks like the second one.
but only using seed once like the first one
0
Then why din't you create just one sample, and print it 10 times ? I'm confused, why do you want to print the same sample 10 times ?
0
its just a simplified case, the one i working on right now consist of a bunch of lists.
0
Then can you share the one you are working on ? Most people will probably wonder the same thing, cause simplfied case means simplfied solution, which is not what you're looking for obviously
0
Generators for random numbers usually have an internal state that you can retrieve and set. In case of Python, that would be the getstate() and setstate() function:
https://docs.python.org/3/library/random.html
Using that state, you could do it the following way:
https://code.sololearn.com/c4mOb369U3O1/?ref=app
I don't really know Python, but this is a common thing in many languages, so this would be a possibility.
0
Iam not sure if I fully understand what you want to do, but I guess you want to shuffle all the lists using the seed or 'order', if that is the case, why don't you shuffle the lines, and then remake the lists ? It will look like this (assuming all the list have the same lenght):
lists = [listA, listB, listC]
L = [[lst[index] for lst in lists] for index in range(len(lists[0]))]
r = random.randint(100, 1000) # useless, remove this line
seed(r) # useless, remove this line
shuffle(L)
result = [[lst[index] for lst in L] for index in range(len(L[0]))]
But as you can see here, the random.seed is useless since it's generated from a ranfom number, so just remove it, I hope this is the result you want
0
by using seed i can expect the result of random.
so the seed are act kinda like a key. i store the seed somewhere else to be used later in the program.
0
Then use seed
0
so back to my original question.
seed only affect once, i need to call it again and again each time i need it.
my question is there any way use seed without calling seed() over and over again
0
No there isn't, because seed is a sequence, there is no other way to reset that sequence without using another intermediate function
0
ah thx then