Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomRangewithGap #1305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions extensions/community/RandomRangewithGap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"author": "",
"category": "General",
"extensionNamespace": "",
"fullName": "Random Range with Gap",
"helpPath": "",
"iconUrl": "",
"name": "RandomRangewithGap",
"previewIconUrl": "",
"shortDescription": "Expression to pick a random number with a choice of not picking a range of numbers, for example 1 to 10, but skip 6 to 8.",
"version": "1.0.0",
"description": [
"Expression to pick a random number in the range Min to Max and specifi a gap of MinGap to MaxGap.",
"In an Expression, use `RandomRangewithGap` and set the four values you want to chose separated by commas.",
"",
"Example \"YourVariable\"=RandomRangewithGap::RndRanwGap(1,10,6,8) will pick a random number between 1 and 10 but exclude numbers between 6 and 8."
],
"tags": [
"choose",
"random"
],
"authorIds": [
"zU3PWuEuEwVBiv7O3oipF1zrhBG2"
],
"dependencies": [],
"eventsFunctions": [
{
"description": "RandomrangewithGap.",
"fullName": "Random Range with Gap",
"functionType": "Expression",
"name": "RndRanwGap",
"sentence": "getRandomNumberWithGap",
"events": [
{
"type": "BuiltinCommonInstructions::JsCode",
"inlineCode": [
"//runtimeScene.setBackgroundColor(100,100,240);",
"",
"function getRandomNumberWithGap(min, max, excludeStart, excludeEnd) {",
" // Check if the exclude range is within the min and max range",
" if (excludeStart < min || excludeEnd > max) {",
" console.error(\"Error: Exclusion range is out of bounds\");",
" return null;",
" }",
"",
" // Create an array to hold the valid numbers",
" let validNumbers = [];",
"",
" // Populate the array with numbers excluding the specified gap",
" for (let i = min; i <= max; i++) {",
" if (i < excludeStart || i > excludeEnd) {",
" validNumbers.push(i);",
" }",
" }",
"",
" // Check if there are any valid numbers left",
" if (validNumbers.length === 0) {",
" console.error(\"Error: No valid numbers available\");",
" return null;",
" }",
"",
" // Pick a random number from the valid numbers",
" let randomIndex = Math.floor(Math.random() * validNumbers.length);",
" return validNumbers[randomIndex];",
"}",
"",
"// Example usage",
"let min = eventsFunctionContext.getArgument(\"min\");",
"let max = eventsFunctionContext.getArgument(\"max\");",
"let excludeStart = eventsFunctionContext.getArgument(\"gapmin\");",
"let excludeEnd = eventsFunctionContext.getArgument(\"gapmax\");",
"",
"let randomNumber = getRandomNumberWithGap(min, max, excludeStart, excludeEnd);",
"",
"if (randomNumber !== null) {",
" console.log(\"Random number:\", randomNumber);",
"}",
"",
"// Return random number",
"runtimeScene.getVariables().get(\"GetRandReturnnumber\").setNumber(randomNumber);"
],
"parameterObjects": "",
"useStrict": true,
"eventsSheetExpanded": true
},
{
"type": "BuiltinCommonInstructions::Standard",
"conditions": [],
"actions": [
{
"type": {
"value": "SetReturnNumber"
},
"parameters": [
"Variable(GetRandReturnnumber)"
]
}
]
}
],
"expressionType": {
"type": "expression"
},
"parameters": [
{
"description": "Min range value",
"name": "min",
"type": "expression"
},
{
"description": "Max range value",
"name": "max",
"type": "expression"
},
{
"description": "Start of Gap range",
"name": "gapmin",
"type": "expression"
},
{
"description": "End of Gap range",
"name": "gapmax",
"type": "expression"
}
],
"objectGroups": []
}
],
"eventsBasedBehaviors": [],
"eventsBasedObjects": []
}