Ask Your Question

Pb with an integer random draw

asked 2020-05-31 15:15:21 -0500

Cyrille gravatar image

The following code do nearly what I expect : defining random numbers whose sum equal a given number. Unfortunately, if i change RR for ZZ it return an error --- and of course I need integers.

candidats= 4 
nn=list([i for i in range(candidats-1)])
PP=([0 for i in nn])
for k in nn:
    PP[k]=RR.random_element(0,elec-sum(PP[j]for j in nn[:k]))

I do not understand my error since it was working at the beginning. Also the sum was nearly always equal to elec with some time a unit of difference. Also is there a command to shuffle the number inside the list.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-05-31 18:23:14 -0500

Cyrille gravatar image

I was persuaded that ZZ was defined by default as RR. In fact I must add ZZ = IntegerRing() and replace RR by ZZ.

edit flag offensive delete link more


ZZ is defined by default:

sage: ZZ
Integer Ring

Maybe you overwrrote it ?

tmonteil gravatar imagetmonteil ( 2020-05-31 18:38:53 -0500 )edit

Certainly since at the begining my code was working. tmonteil thanks to always answer my studip questions.

Cyrille gravatar imageCyrille ( 2020-06-01 08:32:37 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2020-05-31 15:15:21 -0500

Seen: 34 times

Last updated: May 31