# Pb with an integer random draw

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.

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


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 close merge delete

Sort by » oldest newest most voted

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

more

ZZ is defined by default:

sage: ZZ
Integer Ring


Maybe you overwrrote it ?

( 2020-06-01 01:38:53 +0200 )edit

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

( 2020-06-01 15:32:37 +0200 )edit