Values in the random output array (asked by a friend of the garden)

Source: Internet
Author: User

1, random output of all values in the array (not duplicated)

Static voidMain (string[] args) {            int[] Array = {1,2,3,4,5 }; int[]str=New int[Array.          Length]; Random R=NewRandom ();  for(inti =0; I < array. Length; i++)          {              intindex =R.next (array.              Length); //determine if it already contains              if(str. Contains (Array[index])) {i--; Continue; } Str[i]=Array[index]; //output directly from the array, you can use the following for loop//Console.WriteLine (Str[i]);          }           for(intj =0; J < Str. Length; J + +) {Console.WriteLine (str[j]); } console.readkey ();
View Code

The effect is as follows

2, the specified number in the random output array (not duplicated)

You just have to change the top slightly.

Static voidMain (string[] args) {            int[] Array = {1,2,3,4,5 }; int[]str=New int[3]; Random R=NewRandom ();  for(inti =0; I <3; i++)          {              intindex =R.next (array.              Length); //determine if it already contains              if(str. Contains (Array[index])) {i--; Continue; } Str[i]=Array[index]; //output directly from the array, you can use the following for loop//Console.WriteLine (Str[i]);          }           for(intj =0; J < Str. Length; J + +) {Console.WriteLine (str[j]); } console.readkey ();
View Code

The effect is as follows

3, a value in the random output array (JavaScript version)

<! DOCTYPE html>var array = [1, 2, 3, 4, 5];         var index = Math.floor ((math.random () * array.length));        Alert (Array[index]);     </script>
View Code

The effect is as follows

Values in the random output array (asked by a friend of the garden)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.