Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

how to generate a random 10 digit integer with 1 at last place?

asked 5 years ago

In sage, how to generate a random 10 digit integer with 1 at last place?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
0

answered 5 years ago

slelievre gravatar image

updated 5 years ago

There are many ways.

Here is a simple one using randint:

sage: 10 * randint(10^8, 10^9 - 1) + 1
Preview: (hide)
link
0

answered 5 years ago

dan_fulea gravatar image

updated 5 years ago

We can simply generate a random nine-digits integer, then insert a one at the "last" place. Here is a possibility using the random package:

sage: import random                                                                                                                 
sage: a = random.choice(range(10^8, 10^9)) * 10 + 1                                                                                 
sage: a                                                                                                                             
9768350121

Or we offer directly the corresponding range to the random.choice method:

sage: random.choice(range(10^9 + 1, 10^10, 10))                                                          
8455325531

Here, range(10^9 + 1, 10^10, 10)is a range-object, which "consumed" points to the integers taken from 109+1 with step 10 going up to maximally (and excluding) 1010. Then random.choice called with this range instance picks one random element of the corresponding list.

Preview: (hide)
link
0

answered 5 years ago

Sébastien gravatar image

updated 5 years ago

There is also randrange from the random library which is good for that task:

sage: import random
sage: random.randrange(10^9+1, 10^10, 10)
6071633551
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 5 years ago

Seen: 434 times

Last updated: May 23 '20