I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.
For example:
I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8,9.
How must I go about this?
10 3672
I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.
For example:
I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8,9.
How must I go about this?
you can use the random module. Look here and here
I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.
For example:
I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8,9.
How must I go about this?
>>> import random as rand
>>> help(rand)
Help on module random:
NAME
random - Random variable generators.
FILE
d:\python24\lib\random.py
>>> rand.sample([1,2,3,4,5,6,7,8,9], 5)
[8, 4, 5, 7, 6]
>>>
bvdet 2,851
Expert Mod 2GB
I'm making an advanced random number generator, and I want to be able to give it a set amount of digits and from there it can use the digits 1-9 for the result. I might also want to make a logfile.
For example:
I want it to generate 5 numbers using the digits 1,2,3,4,5,6,7,8,9.
How must I go about this?
- >>> import random
-
>>> seq = [1,2,3,4,5,6,7,8,9]
-
>>> n = 7
-
>>> int(''.join([str(random.choice(seq)) for i in range(n)]))
-
3683319
-
>>>
- >>> import random
-
>>> seq = [1,2,3,4,5,6,7,8,9]
-
>>> n = 7
-
>>> int(''.join([str(random.choice(seq)) for i in range(n)]))
-
3683319
-
>>>
That's very nice. How 'bout -
>>> int("".join(str(i) for i in rand.sample([1,2,3,4,5,6,7,8,9], 5)))
bvdet 2,851
Expert Mod 2GB
That's very nice. How 'bout -
>>> int("".join(str(i) for i in rand.sample([1,2,3,4,5,6,7,8,9], 5)))
I am stuck in 2.3, so no generator expressions - only list comprehensions. random.sample() works great except it will not reuse the numbers and you cannot ask for more numbers than are in the sample list.
bvdet 2,851
Expert Mod 2GB
I am stuck in 2.3, so no generator expressions - only list comprehensions. random.sample() works great except it will not reuse the numbers and you cannot ask for more numbers than are in the sample list.
Here's what I had in mind: - >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
-
>>> i
-
71725441469245314962196254882137L
-
>>> int_format(str(i))
-
'71,725,441,469,245,314,962,196,254,882,137'
-
>>>
Here's what I had in mind: - >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
-
>>> i
-
71725441469245314962196254882137L
-
>>> int_format(str(i))
-
'71,725,441,469,245,314,962,196,254,882,137'
-
>>>
So, what library (your own?) are those functions from?
bvdet 2,851
Expert Mod 2GB
So, what library (your own?) are those functions from?
It's somethig I wrote: - import random
-
-
def int_format(s):
-
if len(s)<=3:
-
return s
-
else:
-
outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
-
outList.reverse()
-
return ','.join([x for x in outList if len(x) > 0])
-
-
def varRandom(dList, n):
-
num = ''.join([str(random.choice(dList)) for i in range(n)])
-
while num.startswith('0'):
-
num = num[1:]
-
num += str(random.choice(dList))
-
return int(num)
-
-
i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
-
print i
-
print int_format(str(i))
-
-
print int_format('12345678923')
-
print int_format('1234567892')
-
print int_format('123456789')
-
print int_format('12345678')
-
-
'''
-
>>> 46010601035631449157303537548986
-
46,010,601,035,631,449,157,303,537,548,986
-
12,345,678,923
-
1,234,567,892
-
123,456,789
-
12,345,678
-
>>>
-
'''
This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit: - def int_format(s):
-
if len(s) <= 3:
-
return s
-
return int_format(s[:-3]) + "," + s[-3:]
I wanted to write my own but could not match the elegance.
It's somethig I wrote: - import random
-
-
def int_format(s):
-
if len(s)<=3:
-
return s
-
else:
-
outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
-
outList.reverse()
-
return ','.join([x for x in outList if len(x) > 0])
-
-
def varRandom(dList, n):
-
num = ''.join([str(random.choice(dList)) for i in range(n)])
-
while num.startswith('0'):
-
num = num[1:]
-
num += str(random.choice(dList))
-
return int(num)
-
-
i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
-
print i
-
print int_format(str(i))
-
-
print int_format('12345678923')
-
print int_format('1234567892')
-
print int_format('123456789')
-
print int_format('12345678')
-
-
'''
-
>>> 46010601035631449157303537548986
-
46,010,601,035,631,449,157,303,537,548,986
-
12,345,678,923
-
1,234,567,892
-
123,456,789
-
12,345,678
-
>>>
-
'''
This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit: - def int_format(s):
-
if len(s) <= 3:
-
return s
-
return int_format(s[:-3]) + "," + s[-3:]
I wanted to write my own but could not match the elegance.
A slight modification might be an improvement: - def int_format(s, sep=","):
-
if len(s) <= 3:
-
return s
-
return int_format(s[:-3]) + sep + s[-3:]
for use in Europe where the comma is not so common.
BTW, how do you like your new status?
bvdet 2,851
Expert Mod 2GB
A slight modification might be an improvement: - def int_format(s, sep=","):
-
if len(s) <= 3:
-
return s
-
return int_format(s[:-3]) + sep + s[-3:]
for use in Europe where the comma is not so common.
BTW, how do you like your new status?
No pressure, huh? I noticed it today and assumed there was a problem with the website. I was thinking it should be TBE (training to be an expert).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brandon Michael Moore |
last post by:
I'm trying to test a web application using a tool written in python. I
would like to be able to generate random values to put in fields. I would
like to be able to generate random dates (in a...
|
by: Sonoman |
last post by:
Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I...
|
by: Joe |
last post by:
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.
I...
|
by: Ben Pfaff |
last post by:
One issue that comes up fairly often around here is the poor
quality of the pseudo-random number generators supplied with many
C implementations. As a result, we have to recommend things like...
|
by: Peteroid |
last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible...
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a...
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: TTCEric |
last post by:
This will be original. I promise.
I cannot get the random number generator to work.
I tried seeding with Date.Now.Milliseconds, it still results in the same values.
What I have are arrays...
|
by: raylopez99 |
last post by:
For the public record.
RL
public void IterateOne()
{
Random myRandom = new Random(); //declare Random outside
the iteration
for (int j = 0; j < Max; j++)
{
|
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...
|
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...
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |