Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
Genera aleatoriamente += o -= javascript

Hola, estoy tratando de asignar += o -= a una posición de objeto. En primer lugar, tengo matemáticas aleatorias eligiendo un número:

 var min = 9; var max = 11; var pos = Math.floor(Math.random() * (max - min + 1)) + min;

Estoy tratando de agregar el resultado de pos a this._target.position.z y this._target.position.x pero quiero que se agregue o reste al azar

por lo que el resultado aleatorio debería ser para la posición Z debería ser:

 this._target.position.z += pos;

o

 this._target.position.z -= pos;

y luego para la posición X debería ser:

 this._target.position.x += pos;

o

 this._target.position.x -= pos;

gracias de antemano.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Si desea hacer aleatoriamente x += value o x -= value , simplemente puede usar la primera versión y luego multiplicar su valor por -1 si desea hacer un -= .

En otras palabras:

 const isNegative = // however you determine this, eg. Math.random const newValue += (isNegative ? -1 : 1) * value;

O, para volver a aplicarlo a su caso, usando la sugerencia de @Chris G en los comentarios:

 const oneOrNegativeOne = (Math.floor(Math.random() * 2) * 2 - 1); this._target.position.x += oneOrNegativeOne * pos;
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar una condición basada en un evento aleatorio como este:

 if (Math.floor(Math.random() * 2)) { //the condition return 0 or 1 for false or true this._target.position.z += pos; } else { this._target.position.z -= pos; }

y ya sea para this._target.position.x

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!