Randomly pick three unique numbers from total six numbers

Randomly pick three unique numbers from total six numbers

  


There's a requirment ask to randomly pick up three unique numbers from total six numbers.

Does anybody know is there an existing function for this ?

If no, can sugguest how should I build the logic ?

For example, there are six numbers, 1 2 3 4 5 6. I need pick up there of them like 123,234 but can not be 122 or 223.


Solution

Hi Zhi,

Suggestion:

  • Insert the six numbers in a List (NumbersList)
  • Assign Index = 0
  • Add a cycle where you will do:
    • RandomNumber = random number between 0 and 5-Index (you can use this component to do this: https://www.outsystems.com/forge/1836/)
    • Append the number that is on position RandomNumber of the NumbersList (NumbersList[randomNumber]) to a new List 
    • Remove that element from the NumbersList
    • Increment Index
    • If Index < 3 continue cycle
  • The three numbers will be in the new list

I haven't tried but I would say that that's about it.

Cheers,

José

Solution

Zhi Li wrote:


There's a requirment ask to randomly pick up three unique numbers from total six numbers.

Does anybody know is there an existing function for this ?

If no, can sugguest how should I build the logic ?

For example, there are six numbers, 1 2 3 4 5 6. I need pick up there of them like 123,234 but can not be 122 or 223.


Where are your six numbers?  In a Javascript array?  A List?


Davidk wrote:

Zhi Li wrote:


There's a requirment ask to randomly pick up three unique numbers from total six numbers.

Does anybody know is there an existing function for this ?

If no, can sugguest how should I build the logic ?

For example, there are six numbers, 1 2 3 4 5 6. I need pick up there of them like 123,234 but can not be 122 or 223.


Where are your six numbers?  In a Javascript array?  A List?


Those six numbers from user input,and after they key in those numbers and click button, system should output all the possible numbers.

As the numbers are input, add them to a Javascript array.  Then you could do this...

var n = 3;
var myArray = [1,2,3,4,5,6];
var shuffled = myArray.sort(function(){return .5 - Math.random()});
var selected=shuffled.slice(0,n);
var selOut = Number(selected.join(""));

selOut will be 3 unique numbers from the array.

Thanks to a post on StackOverflow by Abdennour Toumi.


with the solution of José Costa you do not need JavaScript.   

Is using Javascript in OutSystems something I shouldn't be doing?

I'm really new to OutSystems and so I don't know the underlying structures of the environment or the use cost and weight of various widgets.  I was just pushing out another idea.

By the same token, using a bit of Javascript, you don't need to use 2 Lists.

Javascript can be used whenever you want or need, but if you have a full OS solution, it may be easier for others to read.


Thanks Nuno.  Could I trouble you to put the Javascript solution into the context of a full OS solution?  My being new to OS, and given the original info from the Post and this further detail... "Those six numbers from user input,and after they key in those numbers and click button, system should output all the possible numbers." what would a full OS solution look like?  I find that one way of learning an environment is by researching and posting possible answers to forum posts.  If I'm going to attempt to do that, I'd like to follow the best process.

Davidk wrote:

Thanks Nuno.  Could I trouble you to put the Javascript solution into the context of a full OS solution?  My being new to OS, and given the original info from the Post and this further detail... "Those six numbers from user input,and after they key in those numbers and click button, system should output all the possible numbers." what would a full OS solution look like?  I find that one way of learning an environment is by researching and posting possible answers to forum posts.  If I'm going to attempt to do that, I'd like to follow the best process.

Hi David,

One strategy is client side. The response comes from the browser it self and there is no need for a request to the server. The response is also in the same instant. The other is server side. The question is: If you want to code JS, you are not using what Outsystems brings you. Development speed, maintability and also a low code approach. In the JS way, you need someone with that knowledge. Personally, I always think in a "Outsystems" way. I only change that approach if there is a need for it.


Thanks Miguel.  Understood... it's a transition process I'm going through.  I know Javascript... but I'm just getting to know OutSystems... so I try to approach in an OS way, but I do fall back to code.  I never could have imagined the approach José suggested... because I'm just not familiar with OS yet... and I am going to try that solution.

Your mention of client-side and server-side prompts a question.  Is using JS in OS considered client-side or server-side?  And... is using José's approach considered client-side or server-side?

Thanks.

Davidk wrote:

Thanks Miguel.  Understood... it's a transition process I'm going through.  I know Javascript... but I'm just getting to know OutSystems... so I try to approach in an OS way, but I do fall back to code.  I never could have imagined the approach José suggested... because I'm just not familiar with OS yet... and I am going to try that solution.

Your mention of client-side and server-side prompts a question.  Is using JS in OS considered client-side or server-side?  And... is using José's approach considered client-side or server-side?

Thanks.

Don't forget to see the new videos from Outsystems. They can really help you!

But, Client side development is done almost exclusively in Javascript. The reason JavaScript is called a client side language is because it runs scripts on your computer (Browser).

There are a range of server side languages in use on the web today. Ruby, ASP.NET and many others. They are called server side languages because their scripts are run not on your computer, but on the server which hosts the website and sends down the HTML code.

Answering to your question, José option is server side but it could also be made client side like the other solution you saw :)


Hope I could help you.

Kind regards

Thanks again... good discussion.  One of the reasons I asked about client/server side is because I assumed the Javacsript I wrote within a Javascript Widget was client-side - at least I hoped it was... and it is.  However, it's dangerous to assume... and since Javascript can be run server-side... I just wanted to clarify.  The icons used within OS are well defined for client/server side recognition... but it never hurts to ask.

Davidk wrote:

Thanks again... good discussion.  One of the reasons I asked about client/server side is because I assumed the Javacsript I wrote within a Javascript Widget was client-side - at least I hoped it was... and it is.  However, it's dangerous to assume... and since Javascript can be run server-side... I just wanted to clarify.  The icons used within OS are well defined for client/server side recognition... but it never hurts to ask.

You did well :) Hope I could help you.


hi Zhi Li!

Given:

Find:


Cookbook:

1. Create an Entity

2. Define the widget 'Shake It' action:

where: 

a)

Note: define output structure for this advance query: 



b) 


3. Try It

regards,

IB


Davidk wrote:

Thanks Miguel.  Understood... it's a transition process I'm going through.  I know Javascript... but I'm just getting to know OutSystems... so I try to approach in an OS way, but I do fall back to code.  I never could have imagined the approach José suggested... because I'm just not familiar with OS yet... and I am going to try that solution.

Your mention of client-side and server-side prompts a question.  Is using JS in OS considered client-side or server-side?  And... is using José's approach considered client-side or server-side?

Thanks.

Hi Davidk,

I think it is better to create a new forum thread for this "Javascript" so it does not surrogate this forum topic "Random Number".

regards,

IB 


I agree with I Box. This discussion is very interessing but must be in a different subject

Alberto Ferreira wrote:

I agree with I Box. This discussion is very interessing but must be in a different subject

https://www.outsystems.com/forums/discussion/32693/os-javasript-client-or-server/


Nice ! :)

Zhi Li wrote:

Davidk wrote:

Zhi Li wrote:


There's a requirment ask to randomly pick up three unique numbers from total six numbers.

Does anybody know is there an existing function for this ?

If no, can sugguest how should I build the logic ?

For example, there are six numbers, 1 2 3 4 5 6. I need pick up there of them like 123,234 but can not be 122 or 223.


Where are your six numbers?  In a Javascript array?  A List?


Those six numbers from user input,and after they key in those numbers and click button, system should output all the possible numbers.

How, specifically, are users keying in those numbers?  6 Input fields - 1 number each?  1 input field where the users enter all 6 numbers?  What is being used to input the numbers?  Can the numbers be any number?  e.g. 22 56 1 7 87 125

Thanks.


IB - thanks for posting that solution... very nice.

Here's a web page showing a Javascript solution, utilizing a WebBlock.

https://rune.outsystemscloud.com/RandomNumbers/Home.aspx

Here's the JS function:

SyntaxEditor Code Snippet

function getRandomNumbers(myNumbers, outId){
 
    debugger;
 
    var inNumbers = myNumbers.value;
    
    if(inNumbers == ""){
        outId.innerHTML = "No numbers input";
        return;
    }   
    
    var arrayNumbers = inNumbers.split(" ");
    var n = 3;
    var shuffled = arrayNumbers.sort(function(){return .5 - Math.random()});
    var selected=shuffled.slice(0,n);
    var selOut = Number(selected.join(""));

    outId.innerHTML = selOut;
    
}

And the call to the function is off a button, Extended Properties, onclick event...

SyntaxEditor Code Snippet

"getRandomNumbers(" + inputNumbers.Id + "," + randomThree.Id + "); return false;"

In trying to understand this process, I certainly understand why it's frowned upon.  It's tough to do and feels quite unsupported.  Javascript for the Mobile side is elegantly implemented... but for the Web... not so much.

Also attached is the oml.