ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 01 Jun 2020 15:32:37 +0200Pb with an integer random drawhttps://ask.sagemath.org/question/51661/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.Sun, 31 May 2020 22:15:21 +0200https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/Answer by Cyrille for <p>The following code do nearly what I expect : defining random numbers whose sum equal a given number. Unfortunately, if i change <code>RR</code> for <code>ZZ</code> it return an error --- and of course I need integers. </p>
<pre><code>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))
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?answer=51664#post-id-51664I was persuaded that ZZ was defined by default as RR. In fact I must add ZZ = IntegerRing() and replace `RR` by `ZZ`.Mon, 01 Jun 2020 01:23:14 +0200https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?answer=51664#post-id-51664Comment by tmonteil for <p>I was persuaded that ZZ was defined by default as RR. In fact I must add ZZ = IntegerRing() and replace <code>RR</code> by <code>ZZ</code>.</p>
https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?comment=51665#post-id-51665`ZZ` is defined by default:
sage: ZZ
Integer Ring
Maybe you overwrrote it ?Mon, 01 Jun 2020 01:38:53 +0200https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?comment=51665#post-id-51665Comment by Cyrille for <p>I was persuaded that ZZ was defined by default as RR. In fact I must add ZZ = IntegerRing() and replace <code>RR</code> by <code>ZZ</code>.</p>
https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?comment=51675#post-id-51675Certainly since at the begining my code was working. tmonteil thanks to always answer my studip questions.Mon, 01 Jun 2020 15:32:37 +0200https://ask.sagemath.org/question/51661/pb-with-an-integer-random-draw/?comment=51675#post-id-51675