[EOS HELP] How to get a better randomness? indexOf doesn't work well

Post all technical issues and questions here. We'll gladly help you wherever we can.
Post Reply
Epton
Explorer At Heart
Explorer At Heart
Posts: 111
Joined: Sun Mar 05, 2017 9:56 am

[EOS HELP] How to get a better randomness? indexOf doesn't work well

Post by Epton »

I can't wrap my head around it, the indexOf works great on the page where I used it as IF ["1","3","5"].indexOf(DecHP()) then stuff
But on this specific page I've break it down to IF rest1 == true then ["1"].indexOf(ChoiceJoi1()), ["2"].indexOf(ChoiceJoi1()) etc... and sometimes it would pick 2 but then it would get stuck on choice 1. but never picked 3 or 4.

Is there a better way to initiate a randomness?

Image
https://ibb.co/T2V9Qqm
Tease Preview: https://milovana.com/webteases/showteas ... 0b9c0e8993
kerkersklave
Explorer At Heart
Explorer At Heart
Posts: 552
Joined: Sun Jul 06, 2014 2:11 pm
Gender: Male
Sexual Orientation: Open to new ideas!
I am a: Slave

Re: [EOS HELP] How to get a better randomness? indexOf doesn't work well

Post by kerkersklave »

I'm not quite sure, what you want to do. But I assume, you want to choose between several options with same probability.
The problem is, your ChoiceJoi1() function is reevaluated each time. So what happens is this:
The first if-action is evaluated, if ChoiceJoi1 returns 1 it is taken. If not, the second if action is evaluated, if it is 2 it is taken. If not the third if action is evaluated. And so on. So if you can return values 1 to 6 and have 6 choices, the first choice has probability 1/6, the second 5/6*1/6 so already less and so on. The highest probability falls to the last option with over 40%.

What you have to do, is to evaluate the random function once before the choices so do:

eval: res = ChoicJoi1()
if res == 1 then:
if res == 2 then:
if res == 3 then:


Now to your programming style. You are doing things in a over-complicated way also relying on automatic type conversions.

As I understand it, your ChoiceJoi1() function should just return a number between 1 and 6 or some other limit. Why is there an array involved just turning integers into strings that you don't really need?

I would use a function like that:

Code: Select all

function randomNumber(max) {
  return 1 + Math.floor(Math.random() * max)
}
function randomDice() {
  return randomNumber(6)
}

Then just use it as I've explained before.


And an other thing: if-actions in eos as well as if statements in javascript expect an expression that evaluate to true or something that converts to true. For example positive numbers are converted to true. (It is bad style though, to rely on that, cause the rules are compicated and often not very intuitive).

So it does not make sense to write if(res1 == true), res1 already seems to be a boolean variable, so just write if(res1).
Also if(bla.indexOf(foo)) is bad style as indexOf returns a number (the position of the element or -1), write something like if(bla.indexOf(foo) != -1).

You do not need indexOf anyway thought, as far as I can see.

Hope I could help. Feel free to ask more, if you do not get something. Leaning programming can be a bit overwhelming, especially, if one is trying to learn it from examples while quickly trying to get something done.
Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests