Processing math: 100%
Ask Your Question
1

Generate a list of integer excluding -1, 1 and 0.

asked 2 years ago

ryss gravatar image

updated 2 years ago

In python, I made this code to pick numbers from a list of numbers, excluding -1, 0 and 1. I can't find the equivalent in sage.

def alea(n, max):
    complete_list = [i for i in range(-max, max+1, 1) if i != 0 and i != -1 and i != 1]
    list = random.sample(complete_list, n)
    return(list)

With Sage, I've done this :

sage: def alea(n, max):
....:     list = [randrange(-max, max+1) for i in range(n)]
....:     return list
....: 
sage: a = alea(5,10)
sage: a
[1, 3, 8, -9, 7]

and as you can see, 1 is in the list of course.

My goal will then be to take for example 4 random numbers a, b, c and d, to make a sum of fractions a/b+c/d, and to generate the answer automatically to make a series of exercises in a document written with latex/sagetex. That's why I need to exclude -1, 0 and 1.

Thanks in advance for your help .

edit :

under sage (jupyter) I have this error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-8b055ece5ba6> in <module>
      3     list = random.sample(complete_list, n)
      4     return(list)
----> 5 a = alea(Integer(5), Integer(10))
      6 a

<ipython-input-17-8b055ece5ba6> in alea(n, max)
      1 def alea(n, max):
      2     complete_list = [i for i in range(-max, max+Integer(1), Integer(1)) if i != Integer(0) and i != -Integer(1) and i != Integer(1)]
----> 3     list = random.sample(complete_list, n)
      4     return(list)
      5 a = alea(Integer(5), Integer(10))

AttributeError: 'function' object has no attribute 'sample'
Preview: (hide)

Comments

Why do you need an equivalent and what kind? Your Python code should work fine in Sage as is.

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )

I have an error under Sage, as I show in the "edit" part, and I have some difficulties to understand.

ryss gravatar imageryss ( 2 years ago )

You need to add import random before calling random.sample

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )

Of course ... thanks ... it was so simple. I forgot this.

ryss gravatar imageryss ( 2 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 2 years ago

tmonteil gravatar image

updated 2 years ago

Sage language is Python with some additional functions and classes.

So, Sage has a random function defined, that returns a random number in the interval [0,1]:

sage: random()
0.2808809147593442

If you want to use the sample function from the random python module, you just have tom import it before your Python code:

sage: import random

If you do not want to overwrite the random Sage function by the random Python module, you can do:

sage: from random import sample

but then use sample instead of random.sample.

Preview: (hide)
link

Comments

thx for this answer.

ryss gravatar imageryss ( 2 years ago )

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

Stats

Asked: 2 years ago

Seen: 549 times

Last updated: Jul 08 '22