在本文中,您将会了解到关于在Java中获取随机布尔值的新资讯,同时我们还将为您解释在java中获取随机布尔值的方法的相关在本文中,我们将带你探索在Java中获取随机布尔值的奥秘,分析在java中获取随
在本文中,您将会了解到关于在Java中获取随机布尔值的新资讯,同时我们还将为您解释在java中获取随机布尔值的方法的相关在本文中,我们将带你探索在Java中获取随机布尔值的奥秘,分析在java中获取随机布尔值的方法的特点,并给出一些关于java – 如何从流中获取随机对象、javascript – ES6从对象中获取随机元素而不重复、javascript – 如何在Vue.js中获取随机元素、JavaScript中获取随机数的实用技巧。
本文目录一览:- 在Java中获取随机布尔值(在java中获取随机布尔值的方法)
- java – 如何从流中获取随机对象
- javascript – ES6从对象中获取随机元素而不重复
- javascript – 如何在Vue.js中获取随机元素
- JavaScript中获取随机数
在Java中获取随机布尔值(在java中获取随机布尔值的方法)
好的,我在代码中实现了这个SO问题:随机返回True或False
但是,我的行为很奇怪:我需要同时运行十个实例,每个实例每次运行仅返回一次true或false。令人惊讶的是,无论我做什么,每次我得到false
有什么方法可以改善这种方法,以便我至少有大约50%的机会得到true
吗?
为了使它更易于理解:我将应用程序构建为JAR文件,然后通过批处理命令运行
java -jar my-program.jar
pause
程序的内容-使其尽可能简单:
public class myProgram{
public static boolean getRandomBoolean() {
return Math.random() < 0.5;
// I tried another approaches here,still the same result
}
public static void main(String[] args) {
System.out.println(getRandomBoolean());
}
}
如果我打开10个命令行并运行它,false
每次都会得到结果…
java – 如何从流中获取随机对象
public List<String> createList(int listSize) { Random rand = new Random(); List<String> wordList = sourceWords. stream(). limit(listSize). collect(Collectors.toList()); return wordList; }
那么如何和在哪里可以使用我的随机?
解决方法
随机提供了一些返回流的方法.例如,创建一个随机整数流的int(size).
public List<String> createList(int listSize) { Random rand = new Random(); List<String> wordList = rand. ints(listSize,sourceWords.size()). mapToObj(i -> sourceWords.get(i)). collect(Collectors.toList()); return wordList; }
javascript – ES6从对象中获取随机元素而不重复
意思是如果随机选择蓝色,则无法再次选择.当然,这意味着需要一个默认值.我在考虑使用switch语句.
这是我目前的代码:
const colors = { grey: '#BDC8D1',blue: '#0500FF',pink: '#FF00C7',orange: '#FF7A00' } const randomColor = () => { let keys = Object.keys(colors) return colors[keys[keys.length * Math.random() << 0]] }
解决方法
例如:
// List of all valid values (those that can be returned) const colors = [ 'red','green','blue' ]; // The default color (when all others have been "consumed") const defaultColor = 'black'; // The function that returns a random color const getRandomColor = () => { // At least we return a color let color = defaultColor; // If all colors were prevIoUsly consumed,we won't need // to pick one randomly if (colors.length > 0) { // We select randomly an index from the colors array const index = Math.floor(colors.length * Math.random()); // We store the color to return color = colors[index]; // We remove it from the array colors.splice(index,1); } return color; }; console.log(getRandomColor()); console.log(getRandomColor()); console.log(getRandomColor()); console.log(getRandomColor()); console.log(getRandomColor()); console.log(getRandomColor());
此解决方案的明显问题是您无法多次重复使用您的函数.
更好的解决方案是创建迭代器.每次应用程序的某些部分需要生成随机的一系列颜色时,您将创建一个新的迭代器,并使用其下一个方法来获取新值.
检查以下内容:
// The default color (when all others have been "consumed") const defaultColor = 'black'; const RandomColorIterator = () => { // List of all valid values (those that can be returned) const colors = [ 'red','blue' ]; return { next: () => { // At least we return a color let color = defaultColor; // If all colors were prevIoUsly consumed,we won't need // to pick one randomly if (colors.length > 0) { // We select randomly an index from the colors array const index = Math.floor(colors.length * Math.random()); // We store the color to return color = colors[index]; // We remove it from the array colors.splice(index,1); } return color; },}; }; const iterator1 = RandomColorIterator(); console.log('1:',iterator1.next()); console.log('1:',iterator1.next()); const iterator2 = RandomColorIterator(); console.log('2:',iterator2.next()); console.log('2:',iterator2.next());
我一直在使用箭头功能从父范围中获利.这允许基于每个呼叫访问颜色.
javascript – 如何在Vue.js中获取随机元素
我有3个英雄图像及其内容,我想在用户刷新页面时随机显示它们!
基本上我试图在加载页面上使用Jquery显示随机div,但问题是英雄图像的大小很大并且通过使用Jquery,所有这3个图像开始在DOM中加载,这会影响页面的速度.
在Vue.js中是否有任何解决方案,一次加载一个特定的div,而不是当用户刷新页面时所有3个div!
heading
jQuery代码:
mounted()
{
var random = Math.floor(Math.random() * $('.slider-item').length);
$('.slider-item').eq(random).show();
},
最佳答案
一切都很直接.只需随机化您在Vue中选择的链接即可.
const app = new Vue({
el: '#app',data: {
images: [
'http://via.placeholder.com/350x150','http://via.placeholder.com/200x140','http://via.placeholder.com/200x100'
],selectedImage: ''
},created () {
const idx = Math.floor(Math.random() * this.images.length);
this.selectedImage = this.images[idx]
}
});
总结
以上是小编为你收集整理的javascript – 如何在Vue.js中获取随机元素全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
javascript
JavaScript中获取随机数
js生成随机数
1. 先说几个Math函数
Math.floor() 向下取整
Math.ceil() 向上取整
parseInt() 解析一个字符串,并返回一个整数
Math.random() 获取0-1之间的随机数
Math.round() 四舍五入
2. 获取伪随机数
获取0-9的随机数 parseInt(Math.random() * 10)
获取0-N的随机数 parseInt(Math.random() * N)
获取1-10的随机数 parseInt(Math.random() * 10 + 1)
获取1-N的随机数 parseInt(Math.random() * N + 1)
获取0-N的随机数 parseInt(Math.random() * (N + 1))
获取N-M的随机数 parseInt(Math.random() * (M - N + 1) + N)
用floor()写法和parseInt()一样,用ceil()则再是否+1上会有区别。
关于在Java中获取随机布尔值和在java中获取随机布尔值的方法的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于java – 如何从流中获取随机对象、javascript – ES6从对象中获取随机元素而不重复、javascript – 如何在Vue.js中获取随机元素、JavaScript中获取随机数等相关内容,可以在本站寻找。
本文标签: