472,991 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Random Instance generation

Hello eveybody....

Suppose there is a class A:

I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...

Any ideas.....

Cheers

Balaji
Jul 18 '05 #1
6 1423
Balaji writes:
Suppose there is a class A:
I want to generate say 100 random instance of it...
Define "random instance".

How can I do that....
I want the instance name to be like a1, a2, a3...


If you just want to make 100 instances of a class, try something
like:

class A(object): pass

for count in range(100):
exec "a%s = A()" % count

That is hardly random, though.

--
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com
Jul 18 '05 #2
from basicproperty import common, propertied

class MyObject( propertied.Propertied ):
name = common.StringProperty(
"name", """The "name" of the object by some measure""",
)

instances = [ MyObject( 'a%d'%(i,)) for i in range(100) ]

Doing it without basicproperty defining a "name" would, of course,
require you to determine what you mean by having a "name of" a1 (in
Python, names point to objects from namespaces, they aren't necessarily
attributes of the objects themselves, so two names can point to one
object, or an object can be entirely without a name).

For instance:

instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )

would give you a dictionary with 100 named objects in which you could
then "eval" or "exec" code snippets that rely on those names.
instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )
instances['a1'] <object object at 0x00C7D3C0> eval( '(a2, a8,a4)', instances )
(<object object at 0x00C7D3C8>, <object object at 0x00C7D3F8>, <object
object at 0x00C7D3D8>)

which is the kind of thing you would do if you were trying to write an
interpreter.

Good luck,
Mike

Balaji wrote:
Hello eveybody....

Suppose there is a class A:

I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...

Any ideas.....

Cheers

Balaji

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #3
I don't know what you mean by "random" but one
solution is:

a=[]
a=[A() for n in range(0,100)]

They are a[0], a[1], a[2} and don't resemble
anything like "randomness", but there are 100
of them stored in the list a.

Larry Bates

"Balaji" <ba****@email.arizona.edu> wrote in message
news:49**************************@posting.google.c om...
Hello eveybody....

Suppose there is a class A:

I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...

Any ideas.....

Cheers

Balaji

Jul 18 '05 #4
Balaji wrote:
Hello eveybody....

Suppose there is a class A:

I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...


Usually when you feel that you need to generate instances with
certain names you are on the wrong track in translating the
problem you need to solve into a program.

You can always store 100 instances in a list then iterate
over them.

If you need to identify individual instances by name, create
a data structure (the simplest is a dictionary)
that maps a name to an instance.

Istvan.
Jul 18 '05 #5
ba****@email.arizona.edu (Balaji) wrote in message news:<49**************************@posting.google. com>...
Hello eveybody....

Suppose there is a class A:

I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...

Any ideas.....

Cheers

Balaji

class A: .... pass
.... filter(lambda i:globals().__setitem__('a%d'%i,A()),range(5)) [] a0 <__main__.A instance at 0x00DD8418> a1 <__main__.A instance at 0x00DDB748> a2 <__main__.A instance at 0x00C0DD20> a3 <__main__.A instance at 0x00C0DD50> a4 <__main__.A instance at 0x00DDB1E0>


Instead of globals() you can take any dictionary you want.
Instead of filter you can take list comprehension.
Instead of dictionary you can take a list as container.
Instead of what I posted above you can do what ohters posted.
Pyhton lets you do it in any way of your gusto.

Regards Peter
Jul 18 '05 #6
ba****@email.arizona.edu (Balaji) writes:
I want to generate say 100 random instance of it...

How can I do that....

I want the instance name to be like a1, a2, a3...


You almost certainly want to use an array instead of all those a1,a2,... .
Jul 18 '05 #7

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

Similar topics

10
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...
1
by: David Sworder | last post by:
Hi, Each instance of my client application will generate a 15 character random string the first time it is run. This unique string will be used to identify the client from that point forward....
3
by: Mike Brown | last post by:
I have questions about thread safety in the 'random' module. When using the random.Random class (be it Mersenne Twister or Wichmann-Hill based), is it sufficiently thread-safe (preserving entropy...
5
by: user | last post by:
Hello i call it in almost same time (from two different threads): Random rand = new Random(); id=rand.Next(65535); and i often receive the same results. Is it possible ? How windows count this...
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
10
by: Curt_C [MVP] | last post by:
If I use it in my page it's fine but when I put it in a Class file for calling it returns the same # for each call. Any ideas why? I'm sure it's something I'll slap myself for but the only samples...
2
by: Jim in Arizona | last post by:
I was trying to create a random image generator. I'm using visual web dev express with 2.0 framework. On the web form page (mypage.aspx), I have an image control: <asp:Image ID="Image1"...
4
by: Dimos | last post by:
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...
16
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.