I need random, but why is it not?

This is the place for queries that don't fit in any of the other categories.

I need random, but why is it not?

Postby siestamadokaist » Sat Apr 06, 2013 1:54 pm

I think this is the important part
Code: Select all
from random import randint
def generateParent():
   global li   
   a = [];
   for i in range(0,7):
      data = li[i];     
      v  = randint(0,1)
      data['brought'] = v;
      a.append(data)
   return a;

parentList = [];
for i in range(0,5): #*<- here replaced
   t = generateParent();
   parentList.append(t)

this is the entire source code.
Code: Select all
def newItem(iid, berat, nilai):
   rr = {
      "id" : iid,
      "weight" : berat,
      "value" : nilai,
      'brought': 0
   };
   return rr;

li = [];
a = newItem('b1',5,150);
b = newItem('b2',7,220);
c =newItem('b3',8,300);
d =newItem('b4',3,100);
e =newItem('b5',9,100);
f =newItem('b6',4,40);
g =newItem('b7',13,500);

li.append(a)
li.append(b)
li.append(c)
li.append(d)
li.append(e)
li.append(f)
li.append(g)

from random import randint
def generateParent():
   global li   
   a = [];
   for i in range(0,7):
      data = li[i];      
      v  = randint(0,1)
      data['brought'] = v;
      a.append(data)
   return a;

parentList = [];
for i in range(0,5): #*<- here replaced
   t = generateParent();
   parentList.append(t)

#parentList = [0,0,0,0,0];
for data in parentList:
   print data

apparently this code give me 5 similar parent?
but when I do this instead:
Code: Select all
for i in range(0,5):
   print generateParent()

that printed a different parent, so err, why in the first code it doesn't regenerate parent?
siestamadokaist
 
Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

Re: I need random, but why is it not?

Postby rrashkin » Sat Apr 06, 2013 2:25 pm

I'm not entirely sure what you're trying to do here but random.randint(0,1) only ever returns 0 or 1. Therefore, like flipping a coin, sometimes you'll get a series of 1's or a series of 0's and sometimes they'll alternate but they will never be anything other than 0 or 1.
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: I need random, but why is it not?

Postby siestamadokaist » Sat Apr 06, 2013 2:37 pm

emh, well, yea, that randint(0,1) is representing whether an item is brought or not.
I mean, this is the parent in parentList which is similar each other
(to reduce unsignificant data, I only print the 'brought' value of each parent)

parent 0 is bringing: 0 1 1 1 0 0 0
parent 1 is bringing: 0 1 1 1 0 0 0
parent 2 is bringing: 0 1 1 1 0 0 0
parent 3 is bringing: 0 1 1 1 0 0 0
parent 4 is bringing: 0 1 1 1 0 0 0

1 means item[i] is brought
0 means item[i] is left
siestamadokaist
 
Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

Re: I need random, but why is it not?

Postby stranac » Sat Apr 06, 2013 8:23 pm

You're having problems using(and modifying) mutable types in your function.

data and li[i] are just different names for the same dictionary.
If you modify data, li[i] is also modified.

When appending to a, you're creating another name(a[i]) for that, now modified, dictionary.
You then return a.

Each time you call the generateParent() function, you're creating a new a list.
The new list will contain the dictionaries a[0], ..., a[6].
But, as I just explained, a[i] is just a different name for li[i].

So, all the lists you created contain li[0], ..., li[6].
Even though you created 5 new lists, all of them contain the exact same dictionaries.


I hope now you understand what the problem is, and that it will bring you closer to a solution.

One possibility is creating a new dict, instead of assigning li[i] and changing it.
There are a few ways to do this, but probably the easiest is to use dict.copy()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests