comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: random integers between 0 and 1,000,000
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: random integers between 0 and 1,000,000 [message #45960 is a reply to message #45956] Mon, 24 October 2005 07:04 Go to previous messageGo to previous message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Norbert Hahn wrote:
> "Peter Albert" <peter.albert@gmx.de> wrote:
>
>> Hi Mike,
>>
>> I'd guess you got the negative numbers because you defined the array
>
> Unfortunately your guess does not correspond to the docu:

I don't follow you. How does his guess fail to correspond to the
docu[mentation?]?

>> weights beforehand to be of integer type. Assigning it with values of
>> type long will lead to negative numbers if the values are larger than
>> 2^15-1.
>
> fix uses 15+1 bits for integer numbers (one bit is used for sign)
> long uses 31+1 bits for integer numbers ...
> ulong uses 32 bits for unsigned numbers. There is no sign.
> long64...

Agreed. Therefore, if the code the original poster showed us were
preceeded by the following statement:

weights = intarr(29)

then the expression:

weights[i] = ULONG(1000000 * RANDOMU( seed, 1 ))

takes a 32 bit unsigned long with a value somewhere in the range from 0
to 1000000, and converts it into a 16 bit signed int, with a range from
-32768 to 32767. This is the only way that weights[i] could ever gain a
negative value from that expression. For any other data types, all of
the values would be positive.

> So the original problem comes from interpreting the internal bits of
> unsigned numbers. The preferable function for transforming 0...1 float
> to integer without sign would be long.

Using 'long' rather than 'ulong' wouldn't do any good if the type of
"weights" itself is INT.

Note to original poster: loops are very inefficient in IDL. You'd be
better off writing:

weights[*] = LONG(1000000*RANDOMU(seed, 29))

If the length of "weights" happens to be 29, and not something longer,
you're even better off using:

weights = LONG(1000000*RANDOMU(seed,29))
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Problem: Opening a 20-30MB sized ShapeFile
Next Topic: Keeping Button Pressed In?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:04:05 PDT 2025

Total time taken to generate the page: 0.00412 seconds