Home javascript Random selection from a set of numbers

Random selection from a set of numbers

Author

Date

Category

I apologize to the Internet and I can not find the answer as randomly get one of the specified numbers . For example, I asked 1, 6, 11, 16, 21 … and I need one of them by chance. Help.


Answer 1, Authority 100%

JavaScript :

var arr = new array (100, 101, 102, 103, 104, 105, 106, 107, 108) ; // Some array of numbers
var randelement = arr [math.floor (math.random () * (arr.length)]; // Selection of the Random Element

C #

Suppose that in int [] array, the length of which N, contains all these numbers that you need.

Then with the help of such a code you can get a random of them:

int [] array = new int [n]; // here contain all your numbers, their number n
Random RND = New Random (datetime.Now.seconds); // Generator (pseudo) random numbers
// Arghent Random Designer can take any integer value
INT RANDOMNUMBER = Array [rnd.next (0, N)]; // We get a random element from the array array

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions