[Tutor] seed & random !?
Andrei Kulakov
ak@silmarill.org
Sun, 14 Oct 2001 06:55:53 -0400
On Sun, Oct 14, 2001 at 03:45:52AM -0700, necati kaya wrote:
> Thank you for your suggestion, but it is half of what
> I need:
> I want to create random lists that have the same
> elements as a given list, but not in the same order
> with original and also with previous generated lists.
Oh.. okay then.. simply append them to a list of random lists and test if
it's there or not.. see below.
>
> What you suggested is really useful for me to create
> the total number of permutations of original list, but
> it gives also previous generated list order:
>
> list_1 : ['1', '2', '3', '4']
>
> 0 -> ['1', '4', '3', '2']
> 1 -> ['4', '3', '2', '1']
> 2 -> ['1', '3', '2', '4'] ___
> 4 -> ['1', '3', '4', '2'] \
> 6 -> ['2', '1', '3', '4'] \ Both are same !!
> 7 -> ['3', '1', '2', '4'] /
> 8 -> ['1', '3', '2', '4'] ___/
> 9 -> ['4', '3', '2', '1']
>
> ################################
> from random import shuffle
> from copy import copy
>
> list_1 = ['1', '2', '3', '4']
lists = [list_1]
> list_2 = copy(list_1)
> print 'list_1 :', list_1
> print
> for i in range(10):
> shuffle(list_2)
if list_2 not in lists:
l = copy(list_2)
lists.append(l)
But.. do you need *all* permutations? If so, there must be a more
efficient and straightforward way to do this..
>
> #################################
>
> Regards,
> umit
>
>
> --- Andrei Kulakov <sill@optonline.net> wrote:
> > On Sun, Oct 14, 2001 at 02:27:21AM -0700, necati
> > kaya wrote:
> > > Hi,
> > > I am trying to generate different new_list
> > randomly in
> > > each step of the loop from original_list, but I
> > can
> > > not. As the example;
> > > original_list=['1','2','3','4'] and my program
> > produce
> > > those new_lists randomly, but
> > >
> > > ['4', '2', '3', '1'] ==>
> > > ['1', '4', '2', '3'] Both are same
> > > ['4', '2', '3', '1'] ==>
> > > ['3', '2', '1', '4']
> > > ['2', '4', '3', '1']
> > >
> > > I tried to use seed function, but it gave me all
> > the
> > > same results. Do you have any suggestion to have
> > > different new_list which is generated from
> > > original_list in every step.
> >
> > If I understood right, you want to create random
> > lists that have the same
> > elements as a given list, but not in the same
> > order.. if that's true, you
> > can do this with my_list.shuffle:
> >
> > >>> from random import shuffle
> > >>> l
> > [3, 4, 2, 1]
> > >>> from copy import copy
> > >>> l2 = copy(l)
> > >>> shuffle(l2)
> > >>> l2
> > [2, 4, 3, 1]
> > >>> l
> > [3, 4, 2, 1]
> > >>> for i in range(10):
> > ... shuffle(l2)
> > ... if l != l2:
> > ... print l2
> > ...
> > [3, 1, 4, 2]
> > [1, 2, 4, 3]
> > [4, 3, 2, 1]
> > [2, 1, 4, 3]
> > [1, 3, 4, 2]
> > [4, 3, 2, 1]
> > [2, 1, 4, 3]
> > [3, 4, 1, 2]
> > [4, 1, 2, 3]
> > [2, 1, 4, 3]
> > >>> l
> > [3, 4, 2, 1]
> >
> > Instead of printing, you can append it to a list of
> > random lists..
> >
> > Hope that's what you wanted :P
> >
> > > Regards,
> > > umit
> > >
> > > Here is my code:
> > >
> > >
> > > from random import Random
> > > import random
> > >
> > > original_list=['1','2','3','4']
> > > for j in range(0, 5):
> > > temp_list = original_list[:]
> > > new_list=[]
> > > for i in range(0, len(original_list)):
> > > # random.seed()
> > > #??????????????
> > > choosen_item =
> > random.choice(temp_list)
> > > new_list.append(choosen_item)
> > > temp_list.remove(choosen_item)
> > > print new_list
> > > print
> > >
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Make a great connection at Yahoo! Personals.
> > > http://personals.yahoo.com
> > >
> > > _______________________________________________
> > > Tutor maillist - Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> >
> > --
> > Cymbaline: intelligent learning mp3 player - python,
> > linux, console.
> > get it at: cy.silmarill.org
> >
> > _______________________________________________
> > Tutor maillist - Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
>
> __________________________________________________
> Do You Yahoo!?
> Make a great connection at Yahoo! Personals.
> http://personals.yahoo.com
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org