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 CodeThe effect is as follows
Values in the random output array (asked by a friend of the garden)