473,394 Members | 1,751 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,394 software developers and data experts.

Advanced random number generator?

58
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?
Mar 15 '07 #1
10 3672
ghostdog74
511 Expert 256MB
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
Mar 15 '07 #2
bartonc
6,596 Expert 4TB
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]
>>>
Mar 15 '07 #3
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?
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> seq = [1,2,3,4,5,6,7,8,9]
  3. >>> n = 7
  4. >>> int(''.join([str(random.choice(seq)) for i in range(n)]))
  5. 3683319
  6. >>> 
Mar 15 '07 #4
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> seq = [1,2,3,4,5,6,7,8,9]
  3. >>> n = 7
  4. >>> int(''.join([str(random.choice(seq)) for i in range(n)]))
  5. 3683319
  6. >>> 
That's very nice. How 'bout
Expand|Select|Wrap|Line Numbers
  1. >>> int("".join(str(i) for i in rand.sample([1,2,3,4,5,6,7,8,9], 5)))
Mar 15 '07 #5
bvdet
2,851 Expert Mod 2GB
That's very nice. How 'bout
Expand|Select|Wrap|Line Numbers
  1. >>> 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.
Mar 15 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
  2. >>> i
  3. 71725441469245314962196254882137L
  4. >>> int_format(str(i))
  5. '71,725,441,469,245,314,962,196,254,882,137'
  6. >>> 
Mar 15 '07 #7
bartonc
6,596 Expert 4TB
Here's what I had in mind:
Expand|Select|Wrap|Line Numbers
  1. >>> i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
  2. >>> i
  3. 71725441469245314962196254882137L
  4. >>> int_format(str(i))
  5. '71,725,441,469,245,314,962,196,254,882,137'
  6. >>> 
So, what library (your own?) are those functions from?
Mar 16 '07 #8
bvdet
2,851 Expert Mod 2GB
So, what library (your own?) are those functions from?
It's somethig I wrote:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def int_format(s):
  4.     if len(s)<=3:
  5.         return s
  6.     else:
  7.         outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
  8.         outList.reverse()
  9.         return ','.join([x for x in outList if len(x) > 0])
  10.  
  11. def varRandom(dList, n):
  12.     num = ''.join([str(random.choice(dList)) for i in range(n)])
  13.     while num.startswith('0'):
  14.         num = num[1:]
  15.         num += str(random.choice(dList))
  16.     return int(num)
  17.  
  18. i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
  19. print i
  20. print int_format(str(i))
  21.  
  22. print int_format('12345678923')
  23. print int_format('1234567892')
  24. print int_format('123456789')
  25. print int_format('12345678')
  26.  
  27. '''
  28. >>> 46010601035631449157303537548986
  29. 46,010,601,035,631,449,157,303,537,548,986
  30. 12,345,678,923
  31. 1,234,567,892
  32. 123,456,789
  33. 12,345,678
  34. >>>
  35. '''
This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit:
Expand|Select|Wrap|Line Numbers
  1. def int_format(s):
  2.     if len(s) <= 3:
  3.         return s
  4.     return int_format(s[:-3]) + "," + s[-3:]
I wanted to write my own but could not match the elegance.
Mar 16 '07 #9
bartonc
6,596 Expert 4TB
It's somethig I wrote:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def int_format(s):
  4.     if len(s)<=3:
  5.         return s
  6.     else:
  7.         outList = [str(s[max(0,i-3):i]) for i in range(len(s), -1, -3)]
  8.         outList.reverse()
  9.         return ','.join([x for x in outList if len(x) > 0])
  10.  
  11. def varRandom(dList, n):
  12.     num = ''.join([str(random.choice(dList)) for i in range(n)])
  13.     while num.startswith('0'):
  14.         num = num[1:]
  15.         num += str(random.choice(dList))
  16.     return int(num)
  17.  
  18. i = varRandom([0,1,2,3,4,5,6,7,8,9], 32)
  19. print i
  20. print int_format(str(i))
  21.  
  22. print int_format('12345678923')
  23. print int_format('1234567892')
  24. print int_format('123456789')
  25. print int_format('12345678')
  26.  
  27. '''
  28. >>> 46010601035631449157303537548986
  29. 46,010,601,035,631,449,157,303,537,548,986
  30. 12,345,678,923
  31. 1,234,567,892
  32. 123,456,789
  33. 12,345,678
  34. >>>
  35. '''
This function formats long integers using recursion, but I do not know where I got it nor to whom to give credit:
Expand|Select|Wrap|Line Numbers
  1. def int_format(s):
  2.     if len(s) <= 3:
  3.         return s
  4.     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:
Expand|Select|Wrap|Line Numbers
  1. def int_format(s, sep=","):
  2.     if len(s) <= 3:
  3.         return s
  4.     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?
Mar 16 '07 #10
bvdet
2,851 Expert Mod 2GB
A slight modification might be an improvement:
Expand|Select|Wrap|Line Numbers
  1. def int_format(s, sep=","):
  2.     if len(s) <= 3:
  3.         return s
  4.     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).
Mar 16 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
10
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...
3
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...
70
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...
5
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...
104
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...
12
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...
11
TTCEric
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...
16
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++) {
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
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.