Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

random num generator

by florrie (Initiate)
on Jan 20, 2004 at 12:41 UTC ( #322567=perlquestion: print w/replies, xml ) Need Help??

florrie has asked for the wisdom of the Perl Monks concerning the following question:

Hey all need some help please.This may seem very simple to a lot of folk but i need to create a testbench circuit in PERL, as in gate ID, gate type, outputs and inputs.I also need to incorporate a random number generator to call on these elements but it needs to be dynamic data structure.If anybody has any ideas on how to start coding this please mail me. Many thanks

Replies are listed 'Best First'.
Re: random num generator
by Roger (Parson) on Jan 20, 2004 at 12:57 UTC
    You could start with Perl's built-in rand function. Type the following at command-line to get the documentation on rand:

    perldoc -f rand


    What sort of data structure are you thinking about? I would probably store the elements in a hash table with gate ID as lookup key:
    my %gates = ( 'gate001' => { 'gate_type' => 'FLIPFLOP', 'inputs' => [], # array of input values 'outputs' => [], # array of output values }, 'gate002' => { 'gate_type' => 'XOR', 'inputs' => [], # array of input values 'outputs' => [], # array of output values } );

    If I want to insert 20 random input values between (-1.0, 1.0) to gate001:
    for (1..20) { push @{$gates{'gate001'}{'inputs'}}, rand(2.0) - 1.0; }

Re: random num generator
by Abigail-II (Bishop) on Jan 20, 2004 at 12:49 UTC
    Could you explain what's wrong with rand?

    Abigail

Re: random num generator
by hardburn (Abbot) on Jan 20, 2004 at 14:10 UTC

    How "random" are you talking about? Computers are very deterministic, and making them generate high-quality random numbers is quite non-trivial. If you just need a little fudging in your equations, look at the rand suggestions here. If you need something that can't be easily predicted, you'll need Crypt::Random or a similar module.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: random num generator
by barrd (Canon) on Jan 20, 2004 at 12:59 UTC
    Hi florrie,
    Heres how I do it, you will probably get other suggestions too...

    sub generateRandomNumber { my $len = shift || 12; my @chars = (0..9); return join '', map { $chars[ rand @chars ] } 1..$len; }
Re: random num generator
by Theo (Priest) on Jan 20, 2004 at 18:49 UTC
    Sounds like your major problem is setting up a logic gate simulator. Will this be a system wherein you connect many gates together to form complex systems or just a one-gate-at-a-time simulator for demonstrating how AND and OR gates function? Also, does it need to be graphical or will text output be sufficient?

    -Theo-
    (so many nodes and so little time ... )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://322567]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-07-07 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?