Coder Social home page Coder Social logo

rnd.range(1,1) fails about defold-random HOT 6 CLOSED

selimanac avatar selimanac commented on June 25, 2024
rnd.range(1,1) fails

from defold-random.

Comments (6)

selimanac avatar selimanac commented on June 25, 2024 1

@Jerakin I'm closing this for now. If you are having a trouble, please let me know.

from defold-random.

selimanac avatar selimanac commented on June 25, 2024

As expected!
There is no number between min: 1 and max: 1, so what is the actual problem?

from defold-random.

Jerakin avatar Jerakin commented on June 25, 2024

I would expect it to return 1. A random number between 1 and 1 is 1. The same way a random number between 1 and 2 is 1 or 2.

Most random generators I have used (mainly python) have had the following behaviour.

rnd.range(1, 1) -> 1 always
rnd.range(1, 0) -> ValueError

# Meaning that
if min == max:
   return min

if min > max:
  return nil

The change to return nil is a breaking change :)

from defold-random.

selimanac avatar selimanac commented on June 25, 2024

I'll implement this, but my opinion is this not a good practice.
Trying to generate random number by using same value is pointless.
You can simply check your min and max by using if as you wrote.

if min == max:
return min

if min > max:
return nil

from defold-random.

Jerakin avatar Jerakin commented on June 25, 2024

It's for convenience, we noticed this because in our project we use rnd to pick a random element from a table, the table always includes at least one.

With the previous behavior we had implemented this as return myTable[rnd.range(1, #myTable)]. We have have now changed that to do.

if #myTable == 1:
  return myTable[1]
else:
  return myTable[rnd.range(1, #myTable)]

Which works, even though it's a lot uglier. I am fine with either but the breaking change caught us of guard.

from defold-random.

selimanac avatar selimanac commented on June 25, 2024

@Jerakin I was terribly busy for a couple of weeks, sorry.
I add this, could you please test it?

from defold-random.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.