Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`rand` can return invalid values of a `range` type #11015

Closed
dawkot opened this issue Apr 12, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@dawkot
Copy link

commented Apr 12, 2019

import random

echo range[1..4].rand # likely to return something not in the range

dumjyl added a commit to dumjyl/Nim that referenced this issue Apr 15, 2019

@narimiran

This comment has been minimized.

Copy link
Member

commented Apr 15, 2019

Confirmed.


The problem is that range[1..4].rand calls:

proc rand*[T: SomeInteger](t: typedesc[T]): T =
  result = cast[T](state.next)

What you probably wanted to call is proc rand*[T: Ordinal](x: HSlice[T, T]): T, which you can with (1..4).rand.

@narimiran narimiran closed this in 3d0190f Apr 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.