473,847 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Number Generation?

Hello All,

I need some help with random number generation. What I
need exactly is:

To create a few thousand numbers, decimal and
integers, between 5 and 90,
and then to export them as a single column at a
spreadsheet.

I am newbie, I was not able to create decimals with
the random modules of
Python 2.3.

Thanks, Dimos
_______________ _______________ _______________ _____
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Dec 11 '05 #1
4 4126
Dimos <di************ **@yahoo.com> wrote:
Hello All,

I need some help with random number generation. What I
need exactly is:

To create a few thousand numbers, decimal and
integers, between 5 and 90,
and then to export them as a single column at a
spreadsheet.

I am newbie, I was not able to create decimals with
the random modules of Python 2.3.


The random module lets you create floats and integers, not instances of
class decimal.Decimal (surely not in 2.3, which didn't even HAVE a
module decimal in its standard library!). If you want floats, the way
to create a float with uniform distribution between 5 and 90 is to call
random.uniform( 5, 90). If you want 3000:

r3k = [random.uniform( 5, 90) for i in xrange(3000)]

None of the 3k numbers will be integers, and it's unlikely that any of
the 3k floats happens to have a fractional part that's exactly 0. If
you do want integers as well as floats, you'll have to decide with what
probability an integer must appear instead of a float, and do a second
pass on r3k to enforce this.
Alex
Dec 11 '05 #2

Dimos wrote:
Hello All,

I need some help with random number generation. What I
need exactly is:

To create a few thousand numbers, decimal and
integers, between 5 and 90,
and then to export them as a single column at a
spreadsheet.

I am newbie, I was not able to create decimals with
the random modules of
Python 2.3.
You use randint(a,b) to generate an integer between a and b.

For real numbers, the function is random(). But that result is
always between 0.0 and 1.0, so you have to make the range
adjustment yourself.
import random
for i in range(10):

print random.random() *85 + 5

20.2844473176
83.5690712033
77.3459998722
8.79906993754
53.3672450881
25.2609744882
19.8894951301
39.9794852838
43.4056977237
21.7770662903

Thanks, Dimos
_______________ _______________ _______________ _____
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


Dec 11 '05 #3
On Sun, 11 Dec 2005 09:46:33 -0800 (PST), Dimos <di************ **@yahoo.com> wrote:
Hello All,

I need some help with random number generation. What I
need exactly is:

To create a few thousand numbers, decimal and
integers, between 5 and 90,
and then to export them as a single column at a
spreadsheet.

I am newbie, I was not able to create decimals with
the random modules of
Python 2.3.

Others have mentioned random.random and, better for your use case,
random.uniform, but I'm not sure what you mean by "decimal and integers".

Theoretically, the chances of getting an integer from a uniformly random
sample from an interval of real numbers is practically zero, and even
allowing for IEEE 754 double representation, the realtive population of
integers vs non-integers is pretty low. So what do you mean by "integer"?
And what by "decimals"?

If you just want an artificial sprinkling of exact integer values to
happen some percentage of the time, you could do something like
from random import uniform, random
def urnmix(nnum=20, lo=5, hi=90, percentint=25): ... percentint /= 100.
... for _ in xrange(nnum):
... u = uniform(5, 90)
... if random()<percen tint: u = round(u)
... yield u
... for u in urnmix(12): print '%6.3f'%u, ...
9.000 38.173 59.829 37.090 80.504 34.000 69.989 26.000 72.502 64.000 55.000 9.043 for u in urnmix(12): print '%6.3f'%u, ...
10.000 67.687 70.323 66.672 17.150 68.447 84.406 6.997 82.444 8.001 82.946 34.849 for u in urnmix(12): print '%6.3f'%u, ...
70.000 64.000 36.537 75.270 67.000 70.873 28.446 18.483 75.086 41.703 82.885 30.558 for u in urnmix(12): print '%6.3f'%u, ...
75.000 78.313 76.873 48.364 12.000 40.000 36.962 27.704 8.814 44.078 61.000 35.654
Hm, let's check the percentages
[u==int(u) for u in urnmix(12)] [True, False, False, False, False, True, True, True, True, False, True, False] [u==int(u) for u in urnmix(12)].count(True) 2 [u==int(u) for u in urnmix(12)].count(True) 4 [u==int(u) for u in urnmix(10000)].count(True) 2471 [u==int(u) for u in urnmix(10000)].count(True) 2539

Seems to work ...
[u==int(u) for u in urnmix(10000, 5, 90, 5)].count(True) 497 [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True) 111 [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True) 87 [u==int(u) for u in urnmix(10000, 5, 90, 1)].count(True)

113

After all this playing, what was it you actually wanted? ;-)

Regards,
Bengt Richter
Dec 12 '05 #4
bo**@oz.net (Bengt Richter) writes:
Theoretically, the chances of getting an integer from a uniformly
random sample from an interval of real numbers is practically zero,
and even allowing for IEEE 754 double representation,
Well, if we're going to be picky, the chances of getting a number with
an IEEE 754 representation from a uniformly random sample from an
interval of real numbers is practically zero. Of course, this is true
for *any* finite subset of the reals (such as the set of numbers that
have names that can be pronounced in the average human lifespan), and
probably an infinite number of infinite subsets as well.

But I tend to pick irrationals when asked to "pick a number between 1
and 10."
So what do you mean by "integer"?
And what by "decimals"?


I think we should start by finding out what he means by "number",
which is apparently a superset of both what he means by "integer" and
"decimals".

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 12 '05 #5

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

Similar topics

10
11965
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
10
2513
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
10
2914
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 have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
10
741
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of rand(): #include <stdlib.h> #include <time.h>
13
4242
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
22
3465
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
13524
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
8
7564
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
16
539
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers given a seed. - Relatively uniform, does not have to be perfect. The application is not a security or statistics application, the quality of numbers is not a priority although a fairly uniform
0
9727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10982
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10706
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10335
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7880
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5909
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.