//先随机产生一个下标再获取元素
String random = "";
String[] doc = {"成功领取赠险", "安全到家", "已领取50万保障", "和家人团聚","在家中贴春联"};
int index = (int) (Math.random() * doc.length);
random = doc[index];
-
js实现从数组里随机获取元素
2020-10-24 21:47:23主要介绍了js实现从数组里随机获取元素的方法,以及个人封装的js代码分享,十分的实用,这里推荐给小伙伴们 -
JAVA 从数组随机获取一个元素
2019-09-26 20:12:25//先随机产生一个下标再获取元素String random = "";String[] doc = {"成功领取赠险", "安全到家", "已领取50万保障", "和家人团聚","在家中贴春联"};int index = (int) (Math.random() * doc.length);random = doc...转载于:https://www.cnblogs.com/hjy1017/p/6370411.html
-
随机获取数组元素 和 数组元素随机排序
2017-01-22 17:32:00//随机取得数组中的一个元素 function Rand(){ var arr=[11,22,33,44]; var a=Math.floor(Math.random()*arr.length);//Math.floor(Math.random()); //Math.floor()方法执行的是向下取整计算,它返回的是...<script type="text/javascript">
//随机取得数组中的一个元素
function Rand(){
var arr=[11,22,33,44];
var a=Math.floor(Math.random()*arr.length);//Math.floor(Math.random()); //Math.floor()方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。
//alert(Math.random()*arr.length);
//alert(a);
alert(arr[a]);
return arr[a];
}
Rand()//数组元素随机排序:方法一
// var arrs=[11,22,33,44];
// var newArr=arrs.sort(function(){
// return Math.random()>0.5?-1:1;
// })
// alert(newArr)// sort() 方法用于对数组的元素进行排序,对数组的引用。请注意,数组在原数组上进行排序,不生成副本。
// 如果想按照其他标准进行排序,就需要提供比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字。比较函数应该具有两个参数 a 和 b,其返回值如下:
// 若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
// 若 a 等于 b,则返回 0。
// 若 a 大于 b,则返回一个大于 0 的值。//数组元素随机排序:方法二
var arr1=[11,22,33,44];
var arr2=[];
//如果把一个数组所有元素for遍历出来再随机选取一个,那么选取的元素很可能是重复同一个,
//如果随机取过一个元素之后,又能把这个元素删除,那么之后再继续对数组随机取值,就不会出现重复的情况//splice 添加删除数组元素,并返回 删除的 数组元素
//如果arr1长度>0,用splice随机删除一个arr1中的元素,并把这个删除的存到一个新数组里面arr2中,那么arr2是一个作为一个新的随机生成的数组,函数返回这个数组
//alert(arr1.splice(1,1));
function a(){ //while只要指定条件为 true,循环就可以一直执行代码。
while(arr1.length>0){
arr2.push(arr1.splice(Math.floor(Math.random()*arr1.length),1));
}
alert(arr2);
}
a();
//alert(len);
</script>实例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
*{ list-style:none; text-align:center; font-size:14px; color:#fff; font-family:"微软雅黑"}
body{ list-style:none; margin:0; padding:0; border:0;}
html,body{ height:100%}
p,input,li,ul,a,span,button,div,select,p,{ display:inline-block; list-style:none; padding:0; margin:0; text-align:center;outline:none; color:#fff; font-weight:bold; font-size:14px; font-family:"微软雅黑"}
a{width:20px; height:20px; line-height:20px; display:inline-block;}
input{ background:#afa; width:260px; border:0}
input.end{ background:#cf60;}
span{ overflow:hidden; display:block; float:right;margin:0; margin-left:14px;color:#333; cursor:pointer}
div{ margin:0 auto; width:500px;height:480px; overflow:hidden; border:1px solid #fc0; /* background:url(image/a11.jpg) no-repeat center center ; */position:relative}
input{ background:#39C; border-radius:5px;width:160px; height:40px; cursor:pointer; margin:15px}
p{ display:none}
p,ul{ width:100%; height:100%; position:relative; padding:0; margin:0 }
ul li{ width:125px; height:160px; overflow:hidden; float:left; position:absolute;}
#id0{ background:url(image/c1.jpg) no-repeat center center}
#id1{ background:url(image/c2.jpg) no-repeat center center}
#id2{ background:url(image/c3.jpg) no-repeat center center}
#id3{ background:url(image/c4.jpg) no-repeat center center}#id4{background:url(image/c5.jpg) no-repeat center center}
#id5{ background:url(image/c6.jpg) no-repeat center center}
#id6{ background:url(image/c7.jpg) no-repeat center center}
#id7{ background:url(image/c8.jpg) no-repeat center center}#id8{ background:url(image/c9.jpg) no-repeat center center}
#id9{ background:url(image/c10.jpg) no-repeat center center}
#id10{ background:url(image/c11.jpg) no-repeat center center}
#id11{ background:url(image/c12.jpg) no-repeat center center}
</style><script type="text/javascript">
window.οnlοad=function(){
var num=12; //设置 拼图 有多少网格组成
var div=document.getElementById("div");
var input=document.getElementsByTagName("input")[0];
var ul=document.createElement("ul");
ul.setAttribute("id","ul")//给ul设置一个Id
div.appendChild(ul);
var ulWrap=document.getElementById("ul");
console.log(ulWrap);
var lis=[];
var RandomArr=[];
//根据拼图格数,创建对应个数的li;创建的每个Li存到一个数组中
for(var i=0;i<num;i++){
var li=document.createElement("li");
//console.log(li);
lis.push(li);
li.innerHTML=i;//这个地方设置一个innerHtTML方面在console里面查看下面代码是否成功把这些li重新排序
id="id"+i;
li.setAttribute("id",id);
}//吧上面生成的li的新数组随机排序,
while(lis.length>0){
var arrLi=lis.splice(Math.floor(Math.random()*lis.length),1); //splice 随机 删除并返回删除的元素
RandomArr.push(arrLi) ; //把随机删除的元素重新汇总到一个新数组中,即把原数组随机排序;//RandomArr是二维数组
}
console.log(RandomArr);
for(var n=0;n<RandomArr.length;n++){
node=RandomArr[n][0];
//ulWrap.appendChild(RandomArr[n]);这举报,原因是被添加的元素是个数组,不是元素节点
/*Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. */
console.log(RandomArr[n][0]);//RandomArr是二维数组
ulWrap.appendChild(node);
//node.style.left= node.offsetWidth*n + "px";
}
}
</script>
</head>
<body>
<input type="button" value="点击开始">
<div id="div">
<p></p>
</div>
</body>
</html>转载于:https://www.cnblogs.com/js0618/p/6340943.html
-
如何从数组中随机获取元素
2014-05-05 10:36:07在如下数组中,如何随机产生5个元素,并保存到一个字符串中? [img=https://img-bbs.csdn.net/upload/201405/05/1399257300_520857.jpg][/img] -
Javascript 洗牌算法,打乱数组,随机获取元素
2017-05-02 18:11:00//利用洗牌算法Array.prototype.shuffle=function(){ var i,t,m=this.length; while(m){ i=Math.floor(Math.random()*m--); t=this[m]; this[m]=this[i]; this[i]=t; } return this;}var arr=[1,2,3,4,5];...//利用洗牌算法
Array.prototype.shuffle=function(){
var i,t,m=this.length;
while(m){
i=Math.floor(Math.random()*m--);
t=this[m];
this[m]=this[i];
this[i]=t;
}
return this;
}
var arr=[1,2,3,4,5];
console.log(arr.shuffle());
console.log(arr.slice(0,2));转载于:https://www.cnblogs.com/izengbin/p/6797503.html
-
JavaScript - 随机获取数组元素
2020-05-22 18:44:37随机获取数组元素. 2.学习/操作 1.随机获取数组一个元素 var items = ['1','2','4','5','6','7','8','9','10']; var item = items[Math.floor(Math.random()*items.length)]; 2.随机...1.应用场景
随机获取数组元素. 2.学习/操作
1.随机获取数组一个元素
var items = ['1','2','4','5','6','7','8','9','10'];
var item = items[Math.floor(Math.random()*items.length)];
解释:
Math.random()
返回0到1之间的一个伪随机数,可能等于0,但是一定小于1Math.floor
方法返回小于参数值的最大整数(地板值)结合起来:
随机获取一个0-1之间的小数, 然后乘以自身长度, 一定是一个大于0 小于自身长度的一个数. 如上面 长度为10
则获取的一个随机数就为0-10之间的一个数[数组的下标].
最后根据下标获取数组元素值即可
2.随机获取几个元素
function getRandomArrayElements(arr, count) {
var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index; //只是声明变量的方式, 也可以分开写
while (i-- > min) {//console.log(i);
index = Math.floor((i + 1) * Math.random()); //这里的+1 是因为上面i--的操作 所以要加回来
temp = shuffled[index]; //即值交换
shuffled[index] = shuffled[i];
shuffled[i] = temp;
//console.log(shuffled);
}
return shuffled.slice(min);
}
var items = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
console.log( getRandomArrayElements(items, 4) );截图:
解析:
思路就是将新拷贝一份数组, 然后将数组按照获取元素的次数, 每次将元素交换[从倒数第二个开始往前开始, 与随机获取的数组下标元素对调位置], 最后截取数组元素个数 返回结果.
后续补充
...
3.问题/补充
TBD
4.参考
https://www.cnblogs.com/doseoer/p/5728955.html //js随机从数组中取出几个元素 后续补充
...
-
随机获取数组元素
2020-05-20 22:25:33如题,不废话了,直接代码走起 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>... var arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9 -
随机获取数组元素_数组是如何随机访问元素?数组下标为什么从0开始,而不是1?...
2021-01-13 02:10:28搜云库技术团队关注送4000G架构师视频关注数组如何实现随机访问元素什么是数组?数组(Array)是一种线性表数据结构,它用一组连续的内存空间,来存储相同类型的数据。什么是线性表(Linear List)?线性表就是数据排成... -
JS考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素,比如有个数组有100K个元素,从中不重复...
2019-07-18 08:42:35考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素,比如有个数组有100K个元素,从中不重复随机选取10K个元素。 首先我们要快速生成一个巨大的数组 通过Set特性,存放随机数,这里需要注意的是,没有就... -
sampleSize - 从数组中随机获取 n 个元素
2018-01-18 17:43:00从array中获取n个唯一键随机元素。 使用Fisher-Yates算法对数组... 省略第二个参数,n从数组中随机取得 1 个元素。 const sampleSize = ([...arr], n = 1) => { let m = arr.length; while (m) { const... -
Js 随机从数组中获取一个元素
2019-09-17 14:48:34const randomItem = items => items[Math.random() * items.length | 0]; -
从数组中随机获取某一个元素
2019-08-23 14:50:07固定的数组,从中随机选取一个元素出来,并且每次的返回不同 function shuffle(array,num) { var temp = [], temp = array var return_array = [] for(var i=0; i<num; i++){ if(temp.length>0){..... -
如何从JavaScript数组中获取多个随机唯一元素?
2020-08-03 13:32:24JavaScript code to get multiple random unique elements from an array JavaScript代码从数组中获取多个随机唯一元素 <html> <body> <div id="one"> <p>onep> div> <div id="two"> <p>twop> div> <script> ... -
js获取数组任意个不重复的随机数组元素 原创
2020-12-09 17:53:47代码如下:[removed] //从一个给定的数组arr中,随机返回num个不重复项 function getArrayItems(arr, num) { //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组; var temp_array = new Array();... -
随机获取数组元素/随机点名操作demo函数
2020-08-19 10:41:251.随机点名操作 <!DOCTYPE html> <html> <head> <title></title> </head> <body> </body> </html> <script type="text/javascript"> function ... -
scala 获取数组中元素_从Scala中的元素列表中获取随机元素
2020-06-27 20:48:01scala 获取数组中元素We can access a random element from a list in Scala using the random variable. To use the random variable, we need to import the Random class. 我们可以使用随机变量从Scala中的列表... -
高效地随机选取数组中的元素
2016-09-13 12:32:49有前端题目大概是这样的:考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素。 比如有个数组有100K个元素,从中不重复随机选取10K个元素。 为了演示方便我们将数据简化,先给出方案最后再用大点的数据... -
前端面试题:高效地随机选取数组中的元素
2019-11-04 17:44:54有前端题目大概是这样的:考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素。 比如有个数组有100K个元素,从中不重复随机选取10K个元素。 为了演示方便我们将数据简化,先给出方案最后再用大点的数据... -
每日一站:1考虑到性能问题,如何快速从一个巨大的数组中随机获取部分元素
2019-07-18 11:43:21思路如下 先随机获取下标 Math.floor(Math.random() * orgArray.length) 通过下标找到对应的值,且值在样品数组中必须不存在,否则重新在取,这里可以利用set对象的has属性来实现, 同事样品数组的长度不能大于... -
C# 从数组/列表中随机获取N个元素
2020-10-27 18:03:50/// 获取数组随机N个元素 /// </summary> /// <param name="array">指定数组</param> /// <param name="count">获取元素个数</param> /// <typeparam name="T"></... -
python 随机获取数组元素_Python创建二维数组的正确姿势
2020-12-30 01:45:40在用法上,它有点类似数组,因为每个列表都有一个下标,下标从 0 开始。因此,我们可以使用 list[1] 来获取下标对应的值。如果我们深入下列表的底层原理,会发现列表是基于 PyListObject 实现的。PyListObject 是一... -
js在数组arr中随机获取count数量的元素
2019-01-28 11:30:00// 在数组arr中随机获取count数量的元素; const getRandomArrayElements = (arr, num) => { // 新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组; let temp_array = new Array(); ... -
scala 获取数组中元素_从Scala中的元素列表中获取多个唯一的随机元素
2020-06-23 22:06:00scala 获取数组中元素 在Scala中列出 (List in Scala) The list is an immutable collection of elements of the same data type. The thing that makes a List different from an array is that List is Linked ... -
JavaScript数组常用操作(获取随机元素)
2020-04-20 11:01:16获取随机元素 arr[Math.floor(Math.random() * arr.length)]
-
sqlserver2008r2卸载工具包
-
信息安全管理与信息安全体系实践.ppt
-
实现 MySQL 读写分离的利器 mysql-proxy
-
深究字符编码的奥秘,与乱码说再见
-
C++模板函数和lambda表达式的使用
-
杭电OJ 1233 还是畅通工程
-
app软件测试全栈系列精品课程
-
项目管理工具与方法
-
华为机试 质数因子
-
vue3从0到1-超详细
-
Windows系统管理
-
py课程设计.zip
-
【Python-随到随学】 FLask第一周
-
华为1+X——网络系统建设与运维(高级)
-
基于对偶四元数的姿轨耦合动力学模型1.md
-
MySQL 高可用(DRBD + heartbeat)
-
DHCP 动态主机配置服务(在Linux环境下,配置单网段或跨网段提)
-
Flume--相关图例
-
51单片机电子时钟设计.rar
-
数据库面试题【十四、主键使用自增ID还是UUID】