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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: random integers between 0 and 1,000,000 [message #45955] Mon, 24 October 2005 09:38 Go to next message
Norbert Hahn is currently offline  Norbert Hahn
Messages: 46
Registered: May 2003
Member
I'm sorry for my harsh post.

"Peter Albert" <peter.albert@gmx.de> wrote:

> My point is that although IDL allows you to be
> quite sloppy with data types most of the time, you can get problems
> when assigning values to individual elements of an array. In that case,
> the whole array does not automatically adjust its data type.

This is often overlooked. Thank you very much for pointing this out!

Norbert
Re: random integers between 0 and 1,000,000 [message #45956 is a reply to message #45955] Mon, 24 October 2005 09:36 Go to previous messageGo to next message
Norbert Hahn is currently offline  Norbert Hahn
Messages: 46
Registered: May 2003
Member
"James Kuyper" <kuyper@wizard.net> wrote:

> 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.

I took a closer look on what might have gone on. I ran the following
program:

z = randomu(seed,30)
a = long (z*1000000) & print, a
b = ulong (z*1000000) & print, b
i = fix(a)
print, a

I found that a(1) was negative (-20848). So I printed

print, a[1], format="(z8)"
print, b[1], format="(z8)"
print, i[1], format="(z4)"

and got 4AE90 for both a[1] and b[1] and I got AE90 for i[1]

Thus the fix function simply takes the 16 low order bits from either long
or ulong variable and stores it into the result.

Norbert
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 next 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))
Re: random integers between 0 and 1,000,000 [message #45961 is a reply to message #45960] Mon, 24 October 2005 06:23 Go to previous messageGo to next message
peter.albert@gmx.de is currently offline  peter.albert@gmx.de
Messages: 108
Registered: July 2005
Senior Member
I did not suggest using fix(), and using long() would also not help, if
the array itself is defined to be of type integer. Anyway your point
about interpreting internal bits is perfectly correct. However, the
problem is not about transforming float numbers, as he deals with long
numbers all the time. My point is that although IDL allows you to be
quite sloppy with data types most of the time, you can get problems
when assigning values to individual elements of an array. In that case,
the whole array does not automatically adjust its data type.

Cheers,

Peter
Re: random integers between 0 and 1,000,000 [message #45966 is a reply to message #45961] Mon, 24 October 2005 01:59 Go to previous messageGo to next message
Norbert Hahn is currently offline  Norbert Hahn
Messages: 46
Registered: May 2003
Member
"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:

> 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...

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.

Norbert
Re: random integers between 0 and 1,000,000 [message #45968 is a reply to message #45966] Sun, 23 October 2005 23:12 Go to previous messageGo to next message
peter.albert@gmx.de is currently offline  peter.albert@gmx.de
Messages: 108
Registered: July 2005
Senior Member
Hi Mike,

I'd guess you got the negative numbers because you defined the array
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.

Cheers,

Peter
Re: random integers between 0 and 1,000,000 [message #45971 is a reply to message #45968] Sat, 22 October 2005 19:44 Go to previous messageGo to next message
steven.leopardi is currently offline  steven.leopardi
Messages: 2
Registered: October 2005
Junior Member
Not sure why you're getting negative values here, randomu gives numbers
between 0 and 1..., but try this

weights = long(1000000*RANDOMU( seed, 30 ))
help,weights
print,weights

WEIGHTS LONG = Array[30]

849159 15356 438991 564245 872939 652027
127233 367133 577606 326948 279024 421151
564968 521128 605257 133419
512581 827491 231006 287929 870252
26568 630852 541590 872538 240737 513706
184865 955750 911741

RPF
Re: random integers between 0 and 1,000,000 [message #46047 is a reply to message #45956] Mon, 24 October 2005 12:06 Go to previous message
James Kuyper is currently offline  James Kuyper
Messages: 425
Registered: March 2000
Senior Member
Norbert Hahn wrote:
> "James Kuyper" <kuyper@wizard.net> wrote:
>
>> 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.
>
> I took a closer look on what might have gone on. I ran the following
> program:
>
> z = randomu(seed,30)
> a = long (z*1000000) & print, a
> b = ulong (z*1000000) & print, b
> i = fix(a)
> print, a
>
> I found that a(1) was negative (-20848). So I printed

I was unable to reproduce that result. I tested with several million
random numbers, and never once got a negative value from
long(1000000*randomu(seed,N)). Could you identify the value of z[1]
that gave you that value for a[1]?
  Switch to threaded view of this topic Create a new topic Submit Reply
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 15:26:50 PDT 2025

Total time taken to generate the page: 0.00670 seconds