Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Random Number

90 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Clemens

ungelesen,
08.07.2008, 16:37:1508.07.08
an
Hi

Is there any easy way to generate a random number in TCL?
In some way, I wanna force a signal to HIGH for different
amount of times between 10 & 30 ns.

Any ideas?

Thanks!

Jonathan Bromley

ungelesen,
08.07.2008, 16:54:2808.07.08
an
On Tue, 08 Jul 2008 21:37:15 +0100, Clemens wrote:

>Is there any easy way to generate a random number in TCL?
>In some way, I wanna force a signal to HIGH for different
>amount of times between 10 & 30 ns.

Weird - a sudden rash of digital-simulator questions.

Yes, you can generate pseudo-random numbers in Tcl.

But why would you want to, for this purpose? Your
hardware description language (VHDL or Verilog) has
native random number generation, and those functions
will be much easier to integrate into a test
environment than something written in a simulator's
scripting language. HDL code will be portable
across simulators too, unlike tool-specific scripts.

If you really must use Tcl, look at the docs on [expr]
and find out about the rand() and srand() functions.
If you want to use VHDL, look at the IEEE.math_real
package and find out about the UNIFORM() procedure.
If you want to use Verilog, find out about $dist_uniform()
and the numerous other built-in random distribution
functions. comp.lang.vhdl and comp.lang.verilog will
provide a good pool of expertise.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

Jun

ungelesen,
09.07.2008, 06:17:4609.07.08
an

is this a userable rand?

proc rand {m} {
expr {int($m*rand())}
}

Ron Fox

ungelesen,
09.07.2008, 06:33:0009.07.08
an
Define usable? I suspect the Tcl rand function uses the 'usual' linear
congruential pseudo random number generator. As such it simulates
randomness to a 'reasonable' degree, but is, in fact, not random.

See e.g.:
http://www.fourmilab.ch/hotbits/

For a source of real randoms.

See:
http://en.wikipedia.org/wiki/Random_number_generation

For a general introduction to pseudo random number generators.

See
Knuth, The Art of Computer Programming Vol 2 Seminumerical Algorithms

for more than you ever wanted to know about pseudo random number
generators.

My favorite questions to ask, in a lab with a bunch of folks running
long-winded Monte-Carlo simulations, on parallel systems are:
1. Did you seed the random number generator uniquely on all systems in
the cluster?
2. Did you protect yourself against the random number generator looping?

Then when my victim give me a blank look in reply, and I explain what
I mean.. they get this very interesting _really_ worried look.

To paraphrase a credit card commercial that ran here in the U.S.

Hot bits free
_really_ worried look priceless

Ron


--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321

Donal K. Fellows

ungelesen,
09.07.2008, 06:52:3709.07.08
an
Ron Fox wrote:
> Define usable? I suspect the Tcl rand function uses the 'usual' linear
> congruential pseudo random number generator. As such it simulates
> randomness to a 'reasonable' degree, but is, in fact, not random.
[...]

FWIW, Tcl's RNG is seeded differently per interpreter and isn't *too*
terrible, but is not of a quality suitable for either cryptographic or
serious Monte Carlo simulation purposes. If you really care, read the
source (it's the only way to be sure!)

Donal.

suchenwi

ungelesen,
09.07.2008, 07:15:2409.07.08
an
On 8 Jul., 22:37, Clemens <Clem...@hotmail.com> wrote:
> Hi
>
> Is there any easy way to generate a random number in TCL?
> In some way, I wanna force a signal to HIGH for different
> amount of times between 10 & 30 ns.

set ns [expr {10+round(rand()*20)}]

Fredderic

ungelesen,
09.07.2008, 08:08:4509.07.08
an
On Tue, 08 Jul 2008 21:54:28 +0100,
Jonathan Bromley <jonathan...@MYCOMPANY.com> wrote:

> On Tue, 08 Jul 2008 21:37:15 +0100, Clemens wrote:
>> Is there any easy way to generate a random number in TCL?
>> In some way, I wanna force a signal to HIGH for different
>> amount of times between 10 & 30 ns.
> Weird - a sudden rash of digital-simulator questions.

Perhaps it's about a month or two into a new University semester
somewhere? ;)


> But why would you want to, for this purpose? Your
> hardware description language (VHDL or Verilog) has
> native random number generation, and those functions
> will be much easier to integrate into a test
> environment than something written in a simulator's
> scripting language. HDL code will be portable
> across simulators too, unlike tool-specific scripts.

My own very first contact with TCL in a production environment, quite
some years ago now, was just this. VHDL was used for the design of
components, and/or the design as a whole (being just another
"component" in this sense), but simulation was done through commands
provided by the various modules through the TCL interpreter.

Basically, with the testbench implemented in TCL, it was totally
isolated from the design it was testing (except where specifically
designed it to be otherwise), and there wasn't the temptation to use
components in the testbench that didn't have that complete isolation.
Finally, the facilities provided to the two interfaces simply weren't
equally matched; they were provided for different purposes, and they had
different strengths and weaknesses accordingly.

As I said, that was quite some time ago now that I was doing anything
directly with tools of that sort, but the bits and pieces I've done
since (mostly taking a testbench specification or part there-of, and
turning it into a script for someone else to use) suggest that it
still hasn't changed much.


> If you really must use Tcl, look at the docs on [expr]
> and find out about the rand() and srand() functions.

A little helper for simple random numbers I like to keep around is
roughly defined as just:

proc rand {m {n 0}} {
expr {int(($m-$n)*rand()+$n)}
}

From the OP's question "times between 10 & 30 ns", the usage would then
simply be [rand 30 10] (with extra zeros added to flavour).

For my own purposes I actually implement it in two parts;
::tcl::mathfunc::rrand without the int(), as a handy little
in-[expr] helper, and then define [rand] simply as [expr
{int(rrand($m,$n))}].


Fredderic

Fredderic

ungelesen,
09.07.2008, 08:23:1409.07.08
an
On Wed, 09 Jul 2008 11:52:37 +0100,
"Donal K. Fellows" <donal.k...@manchester.ac.uk> wrote:

> Ron Fox wrote:
>> Define usable? I suspect the Tcl rand function uses the 'usual'
>> linear congruential pseudo random number generator. As such it
>> simulates randomness to a 'reasonable' degree, but is, in fact, not
>> random.

> FWIW, Tcl's RNG is seeded differently per interpreter and isn't *too*
> terrible, but is not of a quality suitable for either cryptographic or
> serious Monte Carlo simulation purposes. If you really care, read the
> source (it's the only way to be sure!)

I've seen a couple programs (and subsequently started doing so in my
own) using /dev/u?random where available (primarily on Linux), if not
as their random number source, then simply to seed (and occasionally
re-seed) their regular random number generator.

Just wondering, would it be at all suitable to do something similar in
TCL?


Fredderic

Donal K. Fellows

ungelesen,
09.07.2008, 08:56:1909.07.08
an
Fredderic wrote:
> I've seen a couple programs (and subsequently started doing so in my
> own) using /dev/u?random where available (primarily on Linux), if not
> as their random number source, then simply to seed (and occasionally
> re-seed) their regular random number generator.
>
> Just wondering, would it be at all suitable to do something similar in
> TCL?

It's not really worthwhile. Tcl's RNG really isn't crypto-quality, and
without that, it's no big deal how it's seeded (we use [clock clicks]'s
ultimate source and the thread ID as the sources of randomness). Without
a high quality RNG, seeding from the system entropy source is a waste of
time, effort, and good entropy bits.

Donal.

Jonathan Bromley

ungelesen,
09.07.2008, 14:27:1309.07.08
an
On Wed, 9 Jul 2008 04:15:24 -0700 (PDT), suchenwi wrote:

>> In some way, I wanna force a signal to HIGH for different
>> amount of times between 10 & 30 ns.
>
>set ns [expr {10+round(rand()*20)}]

Polite cough... I spend quite a lot of my time
fixing this common error, which is often made even
by really smart people :-)

rand()*20 has the range 0 to 19.9999999...
round() applied to that expression has uniform
probability of reaching each value in the range
1 to 19, but only half that probability of
reaching either 0 or 20. To see why, consider
the ranges that round() collapses on to each
integer:

0.0 to 0.5 collapses to 0
0.5 to 1.5 collapses to 1
1.5 to 2.5 collapses to 2
....
18.5 to 19.5 collapses to 19
19.5 to 19.99999999 collapses to 20

Only half the real range for the extremes as for
any of the other values!

Try instead
expr { 10 + floor(rand() * 21.0) }

Sorry to nit-pick, but it's bitten me too many
times in the past.....

0 neue Nachrichten