Задать вопрос
Этот вопрос закрыт для ответов, так как повторяет вопрос Как сделать рандомный выбор?
@chpd

Как прописать код, что бы использовался рандомный блок?

Помогите пожалуйста сделать рандомный выбор.

Имеется вот такой код node.js
{"extVersion":"1.28.1","name":"шд","icon":"riGlobalLine","table":[],"version":"1.28.1","drawflow":{"nodes":[{"computedPosition":{"x":100,"y":352.5,"z":0},"data":{"activeInInput":false,"contextMenuName":"","contextTypes":[],"date":"","days":[],"delay":5,"description":"","disableBlock":false,"interval":60,"isUrlRegex":false,"observeElement":{"baseElOptions":{"attributeFilter":[],"attributes":false,"characterData":false,"childList":true,"subtree":false},"baseSelector":"","matchPattern":"","selector":"","targetOptions":{"attributeFilter":[],"attributes":false,"characterData":false,"childList":true,"subtree":false}},"parameters":[],"preferParamsInTab":false,"shortcut":"","time":"00:00","type":"manual","url":""},"dimensions":{"width":192,"height":72},"dragging":false,"events":{},"handleBounds":{"source":[{"id":"8S2q2DJKK4OQnCwEIDINI-output-1","position":"right","x":196.00002985999822,"y":28.00003589320847,"width":16,"height":16}]},"id":"8S2q2DJKK4OQnCwEIDINI","initialized":true,"label":"trigger","position":{"x":100,"y":352.5},"resizing":false,"selected":false,"type":"BlockBasic"},{"type":"BlockBasic","dimensions":{"width":192,"height":72},"handleBounds":{"source":[{"id":"oihq8eo-output-1","position":"right","x":196.00011148816353,"y":28.000093020284414,"width":16,"height":16}],"target":[{"id":"oihq8eo-input-1","position":"left","x":-20.00003340303005,"y":28.000093020284414,"width":16,"height":16}]},"computedPosition":{"x":117.60668616625503,"y":842.7269731249454,"z":0},"selected":false,"dragging":false,"resizing":false,"initialized":true,"data":{"disableBlock":false,"description":"работа","findBy":"cssSelector","waitForSelector":false,"waitSelectorTimeout":5000,"selector":".category-with-counters-column-NIwCb:nth-child(1) > .category-with-counters-item-HDr9u:nth-child(3) > .styles-module-root-QmppR","markEl":false,"multiple":false},"events":{},"position":{"x":117.60668616625503,"y":842.7269731249454},"label":"event-click","id":"oihq8eo"},{"type":"BlockBasic","dimensions":{"width":192,"height":72},"handleBounds":{"source":[{"id":"2b312bg-output-1","position":"right","x":195.99995930989584,"y":27.999979654947918,"width":16,"height":16}],"target":[{"id":"2b312bg-input-1","position":"left","x":-20,"y":27.999979654947918,"width":16,"height":16}]},"computedPosition":{"x":649.2848239482307,"y":954.1892582261826,"z":1000},"selected":true,"dragging":false,"resizing":false,"initialized":true,"data":{"disableBlock":false,"description":"животные","findBy":"cssSelector","waitForSelector":false,"waitSelectorTimeout":5000,"selector":".category-with-counters-column-NIwCb:nth-child(4) > .category-with-counters-item-HDr9u:nth-child(3) > .styles-module-root-QmppR","markEl":false,"multiple":false},"events":{},"position":{"x":649.2848239482307,"y":954.1892582261826},"label":"event-click","id":"2b312bg"},{"type":"BlockBasic","dimensions":{"width":192,"height":72},"handleBounds":{"source":[{"id":"ul41b6p-output-1","position":"right","x":195.99995930989584,"y":28.000081380208336,"width":16,"height":16}],"target":[{"id":"ul41b6p-input-1","position":"left","x":-20,"y":28.000081380208336,"width":16,"height":16}]},"computedPosition":{"x":383.6429136310944,"y":351.0176849976871,"z":0},"selected":false,"dragging":false,"resizing":false,"initialized":true,"data":{"disableBlock":false},"events":{},"position":{"x":383.6429136310944,"y":351.0176849976871},"label":"active-tab","id":"ul41b6p"},{"type":"BlockBasic","dimensions":{"width":192,"height":72},"handleBounds":{"source":[{"id":"feyvw19-output-1","position":"right","x":196.00006103515625,"y":27.999979654947918,"width":16,"height":16}],"target":[{"id":"feyvw19-input-1","position":"left","x":-20,"y":27.999979654947918,"width":16,"height":16}]},"computedPosition":{"x":256.9762469644279,"y":562.6843516643535,"z":0},"selected":false,"dragging":false,"resizing":false,"initialized":true,"data":{"disableBlock":false,"description":"","timeout":20000,"context":"website","code":"function getRandomInt(max) {\n  return Math.floor(Math.random() * max);\n}\n\nconsole.log(getRandomInt(3));\n// Expected output: 0, 1 or 2\n\nconsole.log(getRandomInt(1));\n// Expected output: 0\n\nconsole.log(Math.random());\n// Expected output: a number from 0 to <1\n","preloadScripts":[],"everyNewTab":false,"runBeforeLoad":false},"events":{},"position":{"x":256.9762469644279,"y":562.6843516643535},"label":"javascript-code","id":"feyvw19"}],"edges":[{"id":"vueflow__edge-8S2q2DJKK4OQnCwEIDINI8S2q2DJKK4OQnCwEIDINI-output-1-ul41b6pul41b6p-input-1","source":"8S2q2DJKK4OQnCwEIDINI","sourceHandle":"8S2q2DJKK4OQnCwEIDINI-output-1","target":"ul41b6p","targetHandle":"ul41b6p-input-1","class":"source-8S2q2DJKK4OQnCwEIDINI-output-1 target-ul41b6p-input-1","updatable":true,"type":"custom","selectable":true,"markerEnd":"arrowclosed","data":{},"events":{}},{"id":"vueflow__edge-ul41b6pul41b6p-output-1-feyvw19feyvw19-input-1","source":"ul41b6p","sourceHandle":"ul41b6p-output-1","target":"feyvw19","targetHandle":"feyvw19-input-1","class":"source-ul41b6p-output-1 target-feyvw19-input-1","updatable":true,"type":"custom","selectable":true,"markerEnd":"arrowclosed","data":{},"events":{}},{"id":"vueflow__edge-feyvw19feyvw19-output-1-oihq8eooihq8eo-input-1","source":"feyvw19","sourceHandle":"feyvw19-output-1","target":"oihq8eo","targetHandle":"oihq8eo-input-1","updatable":true,"type":"custom","selectable":true,"markerEnd":"arrowclosed","data":{},"events":{}},{"id":"vueflow__edge-feyvw19feyvw19-output-1-2b312bg2b312bg-input-1","source":"feyvw19","sourceHandle":"feyvw19-output-1","target":"2b312bg","targetHandle":"2b312bg-input-1","class":"connected-edges","updatable":true,"type":"custom","selectable":true,"markerEnd":"arrowclosed","data":{},"events":{}}],"position":[35.608364353693844,-64.7161209287716],"zoom":0.6892190129982216},"settings":{"blockDelay":0,"debugMode":false,"defaultColumnName":"column","execContext":"popup","executedBlockOnWeb":false,"inputAutocomplete":true,"insertDefaultColumn":false,"notification":true,"onError":"stop-workflow","publicId":"","restartTimes":3,"reuseLastState":false,"saveLog":true},"globalData":"{\n\t\"key\": \"value\"\n}","description":"","includedWorkflows":{}}

Т.е есть блок id "oihq8eo" и блок id "2b312bg", они выполняются последовательно друг за другом. Как написать код (javascript) с помощью возможно Math.random(), что бы блок выбирался рандомно. Я не большой специалист, буду очень благодарен если поможете. Сам написать точно не смогу.
  • Вопрос задан
  • 118 просмотров
Подписаться 1 Средний 7 комментариев
Ответы на вопрос 1
Surviavlist
@Surviavlist
Разработчик-любитель на Node.js
Не совсем понятен вопрос. Если ты используешь массив, то получи его случайный объект:
const randomIndex = Math.floor( Math.random() * 5 ) + 1 // случайное число от 0 до 5

В данном случае умножаем Math.random() на 5, так как эта функция возвращает значение от 0 до 1, которое может равняться 0, но никогда не будет равняться 1. То есть мы получим 0, 1, 2, 3 или 4, но не целым числом, поэтому используем Math.floor(), а потом добавляем единицу, таким образом получаем 1, 2, 3, 4 или 5.

Так же можем добавить в прототип массива новую функцию:
Array.prototype.random = function() {
  return this[Math.floor( Math.random() * this.length ) ]
}

const random = [1, 2, 3].random() // выберет 1, 2 или 3 случайнвм образом

Так функция вернёт из своего массива случайное число, основываясь на его длине
Ответ написан
Ваш ответ на вопрос

Вопрос закрыт для ответов и комментариев

Потому что уже есть похожий вопрос.
Похожие вопросы