Jump to content

Recommended Posts

I want to create an array of 1 - X

(X could be 10, 20, 30 or any number)

 

Then I want to randomly choose a number from 1 - X

 

I now want to remove the random choice - and then do the same again twice

 

Question 1: How do I create the array in the first place?

 

Answer: var x=[0,1,2,3,4,5,6,7,8,9,10];

 

I'd much prefer it if I could do something like this:

 

var x=[0...10];

 

This would create the same, except, I don't have to type out all the numbers! And if I needed to increase the number in the array, I could simply just change the last number

 

I've just made the syntax up  :)

I was hoping something similar shorthand existed

Or do I have to do this by creating a loop?

 

Question 2: how do I pop an item from the middle or somewhere other than the beginning or end?

 

I've googled and all I can find is how to remove the first and last items!

 

The way I see it:

 

- Get a solution for popping from the middle of an array

I'm sure I'll find a solution to this if I looked - but is it optimal to be doing it this way?

 

- Instead, maybe I could fill my array in a random order in the first place - and then just pop at the beginning or end??

 

Which is the best solution?

 

Any code to start me off would be great

 

Thanks

 

 

OM

 

What you're looking for:

 

// Add 40 items to your array

$totalNums = 40; // or whatever

$array = array();

for ($i = 1; $i <= $totalNumbs; $i++)

    $array[] = $i;

 

// Echo them out in random order

$count = count($array);

while ($count > 0) {

    $i = rand(0,$count-1);

    echo $array[$i]

    unset($array[$i])

    $count = count($array);

}

 

thanks for that - that's really helpful

 

i have coded the following (before getting your reply about how best to fill up the array):

 

		var numberoffiles = [1,2,3,4,5,6,7,8,9,10];

	var random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1;

	var random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html";

	numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1

	$("#random_load1").load
	(
		random_page_selector, function()
		{
			$("#random_load1").trigger("create");
		}
	);

	random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1;

	random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html";

	numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1

	$("#random_load2").load
	(
		random_page_selector, function()
		{
			$("#random_load2").trigger("create");
		}
	);

	random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1;

	random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html";

	numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1

	$("#random_load3").load
	(
		random_page_selector, function()
		{
			$("#random_load3").trigger("create");
		}
	);

The above code is for use with jquerymobile

I'm unsure how I can optimise the above
I'm unsure how to use any number for the following code:

	$("#random_loadX").load
	(
		random_page_selector, function()
		{
			$("#random_loadX").trigger("create");
		}
	);

 

(X being the select number)

 

Thanks in advance!

 

 

OM

figured it out (i just thought it would be more complex!):

 

 

for(i = 1; i < 4; i++)

{

random_page_index = Math.ceil(Math.random()*numberoffiles.length) - 1;

 

random_page_selector = "random_html_code/random_" + numberoffiles[random_page_index] + ".html";

 

numberoffiles.splice(random_page_index, 1); // only removing one index, thus the 1

 

var divname = "#random_load" + i;

 

$(divname).load

(

random_page_selector, function()

{

$(divname).trigger("create");

}

);

}

 

thanks for the help

Haha, just realized this is in the JS forum.... my post was in PHP.

 

Try this:

 

// Add 40 items to your array
totalNums = 40; // or whatever
var arr = [];
for (var i = 1; i <= totalNumbs; i++) 
    arr[] = i;

// Echo them out in random order
var c = arr.length;
while (c > 0) {
    i = Math.floor(Math.random()*c);
    console.log(arr[i]);
    arr.splice(i,1);
    c = arr.length;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.