I am building an application that generates a view of space with a random number of clickable stars. So far I am generating the stars as so:
const makeStars = () => {
var num = Math.floor(Math.random() * 7 + 2);
return (
<div className="starWrapper">
<Star
name={makeid}
starType={starList[Math.floor(Math.random() * 6 + 1)]}
></Star>
<h2>Star: {makeid()}</h2>
</div>
);
};
This is working great, but I want this function to run a random number of times when the pages loads. Here is my attempt so far
const makeStars = () => {
var num = Math.floor(Math.random() * 7 + 2);
var count = 0
if (count < num) {
window.setTimeout(function() {
return(<div className="starWrapper"><Star name={makeid} starType={starList[Math.floor(Math.random() * 6 + 1)]}></Star><h2>Star: {makeid()}</h2></div>
{() => count + 1})
}, 10);
}
but so far this isn't returning anything and I'm not sure why. I don't know if setTimeout is the best tool for the job, and I'm open to any other suggestions you have.
The full page of code is viewable here.
This is the function that can be ran a random amount of times.
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createstars(randnum) {
If (randnum > 0) {
/** Do logic here **/
createstars((randnum - 1))
}
}
createstars(rand(1,10)) /** set min and max to your liking , the rand function is inclusive **/
Related
I need to generate a random number without repeating a previous number. This is my code:
getRandomInt( max:number, min:number ) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
It gives me the generated random number but keeps repeating the same number.
Example:
1,2,3,4,4
and in the second call:
1,12,3,14,15
You could easely do this:
Object.defineProperty(Array.prototype, 'shuffle', {
value: function() {
for (let i = this.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
[this[i], this[j]] = [this[j], this[i]]
}
return this
}
})
var numArray = Array.from(Array(10).keys()) //generate an array with number from 0 to 10
numArray.shuffle()
the .shuffle() method will randomly take one at a time off the array and print it out.
I want to generate a random number in Javascript 6 times. I've got the random variable declared and properly functioning. I just need to replicate it six times without logging it six separate times.
const randRoll = Math.floor(Math.random() * 10) + 10;
How would I go about this?
The proper way of doing this is using for loop
let randomNumbers = [];
for(let i = 0; i < 6; i++) {
randomNumbers.push(Math.floor(Math.random() * 10) + 10);
}
You can also do something like this
let randomNumbers = new Array(6).fill(0).map((v) => Math.floor(Math.random() * 10) + 10);
To create a random number in Javascript, the math.random() function is used. JavaScript math.random() generates a random decimal value between 0 and 1.
const random_num = Math.floor(Math.random() * 10); console.log(random_num);
As you can see,In the above example, I used the Math.floor() function to round our random number up to become a random integer, used “* 10” to generate a number between “1” and “10”.
If you need generate a number ( 1 to 100 ) you can multiply generated number by 100 and so on “* maxNumberHer”.
Random Number Between Two Values
Math.floor(Math.random() * (max - min) + min)
Between 900 to 1000
const generateRandomNum = (min, max) => Math.floor(Math.random() * (max - min) + min) console.log(generateRandomNum(900, 1000))
about Repeat 6 times
You can use for loop
for (let i = 0; i < 6; i++) { console.log(generateRandomNum(900, 1000)); }
I made a simple code to add two random numbers.
Number 1 is minimum 1000 and maximum 2000.
It changes every 10 seconds.
Number 2 is minimum 1 and maximum 5.
It changes much faster ( delay is random )
The point is to addition number 1 + number 2, and "refresh" the total everytime number 2 changed.
Thanks in advance :)
function bignumber() {
var bignumber = 1000 + Math.floor(Math.random() * 1000);
$('#bignumber').text(bignumber);
return bignumber;
setTimeout(Big, 2300);
}
bignumber()
function addition() {
var rand = Math.floor(Math.random() * (10 - 5 + 1) + 5);
var smallnumber = Math.floor(Math.random() * (5 - 1 + 1) + 1);
var result = ??How to place Number1 here?? + smallnumber;
$('#smallnumber').text(result);
setTimeout(myFunction, rand * 300);
}
addition()
You have to define var bignumber outside function scope and update its value on function one also read value in function two
var bignumber =0 ;
function bignumber() {
bignumber = 1000 + Math.floor(Math.random() * 1000);
......
function addition() {
....
var result = bignumber + smallnumber;
...`
I would like to repeatedly call the changeStyle() function at random intervals
here is my code so far:
this.platform.ready().then(() => {
this.myFunction()
});
myFunction() {
var min = 5,
max = 10;
var rand = Math.floor(Math.random() * (max - min + 1) + min); //Generate Random number between 5 - 10
this.changeStyle()
setTimeout(this.myFunction, rand * 1000);
}
changeStyle() {
console.log('changing style');
this.sensorImage = 'path/to/image1';
setTimeout(() => {
this.sensorImage = 'path/to/image2';
},
2000);
}
the relevant html code is
<img id='35' (click)="changeStyle()"
src="{{sensorImage}}">
ideally what this should do is call the changeStyle() function repeatedly and randomly without any input from me. however, I get a runtime error saying:
'TypeError: this.changeStyle is not a function. (In 'this.changeStyle('')', 'this.changeStyle' is undefined)'
Can you update your setTimeout(this.myFunction, rand * 1000); function to,
setTimeout(this.myFunction.bind(this), rand * 1000);
I believe the issue is related to this context.
You could do something like this,
changeStyle() {
...
...
const randomMilliSeconds = Math.floor( Math.random() * 10 ) * 1000;
setTimeout(() => {
this.changeStyle();
}, randomMilliSeconds)
}
You just need to call changeStyle once, manually or using click event and it will call itself at random intervals.
I have the following script, which is meant to generate 2 different numbers
<script type="text/javascript">
function GenerateRandomNumber2to6No1() {
var min = 2, max = 7;
var random = Math.floor(Math.random() * (max - min + 1)) + min;
return random;
}
var GenerateRandomNumber2to6No1 = GenerateRandomNumber2to6No1();
$('.GenerateRandomNumber2to6No1').html(GenerateRandomNumber2to6No1);
function GenerateRandomNumber2to6No2() {
var min = 2, max = 7;
var random = Math.floor(Math.random() * (max - min + 1)) + min;
return (random !== GenerateRandomNumber2to6No1) ? random: GenerateRandomNumber2to6No2();
}
var GenerateRandomNumber2to6No2 = GenerateRandomNumber2to6No2();
$('.GenerateRandomNumber2to6No2').html(GenerateRandomNumber2to6No2);
</script>
NUMBER 1: <span class = "GenerateRandomNumber2to6No1"></span>
NUMBER 2: <span class = "GenerateRandomNumber2to6No2"></span>
This usually works fine but occasionally the page will load and no numbers will appear. I'm guessing this is when GenerateRandomNumber2to6No2 happens to generate the same number as GenerateRandomNumber2to6No1. I thought that
return (random !== GenerateRandomNumber2to6No1) ? random: GenerateRandomNumber2to6No2(); accounted for this, but perhaps instead of generating another unique number when there's redundancy, it generates no number at all. Is this the case? If so, how can I fix this?
Thanks!
function getRand(){
var array=[2,3,4,5,6,7]
var newArray=[];
for(var i=0;i<2;i++){
var ran=~~(array.length*Math.random());
newArray[i]=array.splice(ran,1)[0]
}
return newArray
}
var ranArray=getRand();
var ran1=ranArray[0];
var ran2=ranArray[1];
console.log(ran1,ran2)
getRand() is a function to give you an array of distinct numbers.Since you only need two,it only loop two times,and give you an array,which you can make use of it.
Back to your code,from what I see,Gno2 should keep running until it got another number,I don't know why.It's the time you try to debug.
First I will try to change the max=2 to max=3,if the code may not work for some number,then you can quickly realize and found out the problem is in these two functions.
Then try to log,console.log(Gno1,Gno2),see what exactly the numbers are,also help you to know where is the problem.
I think this piece of code can help you
var rand1,rand2;
var min = 3;
var max = 6;
do {
rand1 = Math.floor(Math.random() * (max - min)) + min;
rand2 = Math.floor(Math.random() * (max - min)) + min;
}while(rand1 === rand2);
$('.GenerateRandomNumber2to6No1').html(rand1);
$('.GenerateRandomNumber2to6No2').html(rand2);