Log In  

Hello.

I use RND() quite a bit, don't you ?

I have noticed in other game programming languages that you can have a 2nd argument for the rnd() command. Currently Pico-8 does not do this. It's quite useful though. Here's an example:

a=rnd(1,10)

Will for instance choose an integer random number between and including 1 through 10. This:

a=rnd(10,1)

Will give the same results. This however:

a=rnd(-4,4)

Will give you a random integer number between and including -4 to 4.

The function of which:

function rndrange(a,b)
  if (a>b) a,b=b,a
  return a+rnd(b-a+1)\1
end

How about it guys and @zep, do you think this is a good addition for Pico-8, to add a 2nd argument to already existing rnd() function ? Or a new function altogether, rrnd() for Range Random.

P#120076 2022-11-03 16:48 ( Edited 2022-11-03 16:48)

This would be a nice convenience (saves about 5 tokens per use if my math is right), but I think to keep it consistent with the existing rnd() I wouldn't want it to return an integer or be inclusive of the upper bound.

P#120082 2022-11-03 18:39
1

The rnd() is interesting in how it's similar but still distinctly different from the math.random() function in standard Lua. The standard Lua one does as you request, but also has a default of 1 if the first number isn't included and requires that the inputs be integers. It also doesn't return float results unless no inputs are given, and it lacks the table variation, possibly because it's completely unneeded in the context of the feature you're requesting and no token limit.

I think the reason for pico-8's version to differ is pretty obvious though. The table usage is good for limiting tokens, but any of the other normal variations for RNG functions requires distinguishing types. That's great in c/c++ and is fine in standard Lua which already has a weird expectation of users just knowing these things. It's not so great when trying to make an engine that's intuitive for beginners.

I guess my question for this feature would be this: should pico-8 users have to pay attention to when their values are still "integers" and when the values are implicitly converted to "numbers"? Furthermore, should pico-8 users have to even know that those types are different under the hood?

P#120089 2022-11-03 20:22
1

Adding parameter-specific functions for randomness seems unnecessary. It's trivial to wrap rnd() in a function that works with the desired input and provides the desired output. Yes, it would be nice to save the 17 tokens I use in most projects for a random integer in a range. But if that is added, then a non-integer version would also need to be added. Then you've got three functions providing randomness, only one of which might be applicable to some projects.

That said, I feel it would be in the spirit of PICO-8 to have something like dieroll(n) that returns an integer from 1 to n.

P#120090 2022-11-03 20:23

@binaryeye:

How about, dieroll(n) for integer 1-n and dieroll(a,b) for integer a-b ?

Either way I'd still like this random thingie that other gaming languages already have.

Here it is in Blitz:

Here it is in a sample program:

For Pico-8 that would be:

Cart #tagopemana-0 | 2022-11-03 | Code ▽ | Embed ▽ | No License

-- rand.bmx
-- toss a pair of dice.
-- result is in the range 1+1
-- to 6+6.

-- count how many times each
-- result appears.

function _init()

cls()

countt={}

for i=2,12 do
  countt[i]=0
end

for n = 1,3600 do
    toss = rand(1,6) + rand(1,6)
    countt[toss] += 1
end

for toss = 2,12 do
    print (toss.."="..countt[toss])
end

end

function _update()
end

function rand(a,b)
  if (a>b) a,b=b,a
  return a+rnd(b-a+1)\1
end

I had to use array countt[] as count() is already a function in Pico-8.

P#120099 2022-11-03 23:32 ( Edited 2022-11-03 23:44)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 07:30:49 | 0.014s | Q:20