473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Re: Using the Random Module.

You wrote...
>Is there a better way to do that besides doing this:
>>>>random.randint(00000000000000000, 99999999999999999)
09657398671238769
Maybe this?

random.randint(0, 9e16)
--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

Jul 11 '08 #1
5 1194
WDC
On Jul 11, 2:15*pm, Michiel Overtoom <mot...@xs4all.nlwrote:
You wrote...
Is there a better way to do that besides doing this:
>>>random.randint(00000000000000000, 99999999999999999)
09657398671238769

Maybe this?

* * * * random.randint(0, 9e16)

--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillilhttp://www.catb.org/~esr/halloween/halloween4.html
That would work yes, but is there a random() function that would do
that without the attributes? Not trying to be lazy, I just want to
know if there is a built in way.

Thanks Michiel.
Jul 11 '08 #2
On Jul 11, 1:29*pm, WDC <ki4...@gmail.comwrote:
On Jul 11, 2:15*pm, Michiel Overtoom <mot...@xs4all.nlwrote:
You wrote...
>Is there a better way to do that besides doing this:
>>>>random.randint(00000000000000000, 99999999999999999)
>09657398671238769
Maybe this?
* * * * random.randint(0, 9e16)
--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillilhttp://www.catb.org/~esr/halloween/halloween4.html

That would work yes, but is there a random() function that would do
that without the attributes? Not trying to be lazy, I just want to
know if there is a built in way.

Thanks Michiel.
You want a random integer. Is there a range you want it in?

Past a certain point, you'll exceed the granularity of the random
number generator, and some values in the range will never be generated.
Jul 11 '08 #3
On Fri, 11 Jul 2008 12:27:32 -0700, castironpi wrote:
On Jul 11, 1:29Â*pm, WDC <ki4...@gmail.comwrote:
>On Jul 11, 2:15Â*pm, Michiel Overtoom <mot...@xs4all.nlwrote:
You wrote...
Is there a better way to do that besides doing this:
>>>>random.randint(00000000000000000, 99999999999999999)
09657398671238769
Maybe this?
Â* Â* Â* Â* random.randint(0, 9e16)
--
"The ability of the OSS process to collect and harness the collective
IQ of thousands of individuals across the Internet is simply
amazing." - Vinod
Vallopillilhttp://www.catb.org/~esr/halloween/halloween4.html

That would work yes, but is there a random() function that would do
that without the attributes? Not trying to be lazy, I just want to know
if there is a built in way.

Thanks Michiel.

You want a random integer. Is there a range you want it in?

Past a certain point, you'll exceed the granularity of the random number
generator, and some values in the range will never be generated.
In which case you might grab random numbers, and'ing them with 0xffff or
0xffffffff or something at a time, and then smoosh them together with
shifts and or's, or multiplication and addition, to get the precision you
need.

Jul 12 '08 #4
On Fri, 11 Jul 2008 12:27:32 -0700, castironpi wrote:
You want a random integer. Is there a range you want it in?

Past a certain point, you'll exceed the granularity of the random number
generator, and some values in the range will never be generated.
You might want to produce an unbounded random integer, where *every*
integer has a chance to be returned:

def unbounded_randint():
i = 0
while True:
if random.random() < 0.5:
return i
i += 1

This returns 0 with probability 1/2, 1 with probability 1/4, 2 with
probability 1/8, etc. The probability distribution is centered close to
zero (the mode and median are both zero, and I'm too lazy to calculate
the mean) and it has an infinitely long tail.
--
Steven
Jul 12 '08 #5
WDC
On Jul 12, 10:06*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Fri, 11 Jul 2008 12:27:32 -0700, castironpi wrote:
You want a random integer. *Is there a range you want it in?
Past a certain point, you'll exceed the granularity of the random number
generator, and some values in the range will never be generated.

You might want to produce an unbounded random integer, where *every*
integer has a chance to be returned:

def unbounded_randint():
* * i = 0
* * while True:
* * * * if random.random() < 0.5:
* * * * * * return i
* * * * i += 1

This returns 0 with probability 1/2, 1 with probability 1/4, 2 with
probability 1/8, etc. The probability distribution is centered close to
zero (the mode and median are both zero, and I'm too lazy to calculate
the mean) and it has an infinitely long tail.

--
Steven
Thanks Steven. I like how that bit works.

No range in particular. I was just wondering if there was a better way
than what I had been doing. Thanks for help.
Jul 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Code_Dark | last post by:
Hi, sorry to bore you with my newbie questions, but I _am_ a newbie so.. right. Anyway, I was working out a "number guessing game", which works fine, except I wanted to make the number a random...
28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
5
by: Bill | last post by:
Hello, If I type the following code directly into the interpreter, it works. If I run it from a script, it generates the following error. Can someone help? Thanks! ------------------------ ...
3
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
4
by: Harlin Seritt | last post by:
I looked all over the net but could not find if it is possible to insert wildcards into strings. What I am trying to do is this: I am trying to parse text from a Bible file. In case you're not...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
14
by: DataSmash | last post by:
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python...
6
by: Alan Isaac | last post by:
Running test.py will print False. Is this expected/desirable? Thanks, Alan Isaac %%%%%%% test.py %%%%%%%%%%% from random import seed, getstate seed(217) x = getstate()
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
3
by: Andrew F | last post by:
I'm a linux user and I just upgraded from 2.1 to 2.5 and changed the location of a number of libraries and tools. So far I've tracked most errors, but this one has me scratching my head : $...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.