Skip to content

Get-Random unexpectedly outputs [double] values with various integer types #3598

Closed

Description

Steps to reproduce

The following uses variations of Get-Random 1 to produce a number in the interval [0,1), which for any integer type should invariably yield 0:

[sbyte], [byte], [int16], [uint16], [int], [uint32], [int64], [uint64] | % {
  [pscustomobject] @{
    TypeIn = $_.Name
    Result = Get-Random ($_::Parse(1))
    TypeOut = (Get-Random ($_::Parse(1))).GetType().Name
  }
}

Expected behavior

TypeIn            Result TypeOut
------            ------ -------
SByte                  0 SByte 
Byte                   0 Byte 
Int16                  0 Int16 
UInt16                 0 UInt16
Int32                  0 Int32  
UInt32                 0 UInt32 
Int64                  0 Int64  
UInt64                 0 UInt64 

Actual behavior

Something like:

TypeIn            Result TypeOut
------            ------ -------
SByte  0.412813293008512 Double 
Byte   0.117663220557227 Double 
Int16  0.326887441485602 Double 
UInt16 0.502738704207697 Double 
Int32                  0 Int32  
UInt32 0.995645226908683 Double 
Int64                  0 Int64  
UInt64  0.39285415708686 Double 

Note how only [int32] and [int64] correctly output a (type-preserved) integer.

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64

Activity

  1. added
    Issue-Enhancement
    on Apr 19, 2017
  2. iSazonov commented on Apr 19, 2017

    @iSazonov
    Collaborator

    With code side it is "by design" not "unexpectedly". So should we change the design?

  3. mklement0 commented on Apr 20, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    The existing documentation partially contradicts current behavior:

    Enter a 32-bit integer or a double-precision floating-point number, or an object that can be converted to an integer or double

    If we assume that "to an integer" means to an [int32], then certainly the smaller integer types would qualify for that conversion - but that's not what's happening currently.

    Under the same assumption, you could argue that the larger integer types only fit into [double], so they should be treated as that, which is true for [uint64], but not for [int64].

    I think that a change is warranted from a common-sense standpoint, and that the change falls into bucket 3 - unlikely grey area.

  4. iSazonov commented on Apr 20, 2017

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) I mean only that the code make this explicity and it is not bug.
    Is "Expected behavior" that you want to get precisely or you want to get int32 for small "integers"?

  5. mklement0 commented on Apr 20, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov): That's fine, as long as there's agreement that the behavior is counter-intuitive (which is what I meant by "unexpected").

    I think getting the precise type back makes sense, but I'm not sure I'm aware of all the ramifications.

    (As an aside: It's open to debate whether the source code reflects deliberate intent to treat the less common integer types not explicitly mentioned as [double], or whether that was a mere oversight.)

  6. added
    Documentation Needed in this repo
    on Aug 3, 2017
  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. added
    Resolution-No Activity
    on Nov 16, 2023
  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions