1. pick a random number, that hasn't already been picked?

ok.. basically what i am doing is trying to pull 5 random lines of text from an array, and printing it onto the page. I wrote this code to do it:

Code:
```var featopp = new Array();
featopp[0] = new Array ( "Sample Text 01", "Sample Text 01a" );
featopp[1] = new Array ( "Sample Text 02", "Sample Text 02a" );
featopp[2] = new Array ( "Sample Text 03", "Sample Text 03a" );
featopp[3] = new Array ( "Sample Text 04", "Sample Text 04a" );
featopp[4] = new Array ( "Sample Text 05", "Sample Text 05a" );

function featopp_write() {
for (i=1; i<=5; i=i+1)
{
var num=Math.round(Math.random()*(featopp.length-1));
document.write(featopp[num][0] + "<BR>")
}

}```

Now my question is, how can i make it so instead of just picking a random number, it picks a random number that is hasn't picked before. Basically I don't want any of the text displayed to repeat.

I was thinking of creating another array and storing which numbers have already been used. which i was able to create, but i was unsure of how to check if a number already exsisted in the array, and if it did try another random number.

2. but i was unsure of how to check if a number already exsisted in the array
Something like this:
Code:
```for(var i = 0; i < myarray.length; i++)
{
if (myarray[i] == chosenNum)
}```

3. featopp[0] = new Array ( "Sample Text 01", "Sample Text 01a" );
A short cut that avoids that "new Array" stuff is this:

featopp[0] = ["Sample Text 01", "Sample Text 01a"];

That's called an "array literal"--kinda the counter part to a "string literal". A string literal is anything between quotes: e.g. "some text". You could always create strings like this:

var str = new String("hello world");

but that's a pain, so you usually do this:

var str = "hello world";

You can do the same thing with arrays.

4. When you're done with your script post it, and I'll show you a tricky way to do what you want.

5. okie.. i'm still having trouble.. this is the best i could come up with, and it still doesn't work correctly.

Code:
```
var used = new Array(5);
var tryagain = 0
function featopp_write()
{
for (i=1; i<=5; i=i+1)
{
var num=Math.round(Math.random()*featopp.length-1)
used[i] = num
for (j=1; j<=5; j=j+1)
{
if (used[j-1] = num)
{tryagain = 1}
}
if (tryagain = 1)
{i = i-1}
else
{
document.write(featopp[num][0] + "<BR>")
tryagain = 0
}
}

}```

6. can i get a little explanation on how i can make that one work?
Remember, been a long time since I have written javascript...

7. Code:
```var used = new Array(5);
var tryagain = 0
function featopp_write()
{
for (i=1; i<=5; i=i+1)
{
var num=Math.round(Math.random()*featopp.length-1)
used[i] = num
for (j=1; j<=5; j=j+1)
{
if (used[j-1] = num)
{tryagain = 1}
}
if (tryagain = 1)
{i = i-1}
else
{
document.write(featopp[num][0] + "<BR>")
tryagain = 0
}
}

}```
1) In this line:

var used = new Array(5);

It's easier to just do this:

var used = []; //a new blank array

2)for (i=1; i<=5; i=i+1)

That is commonly written as i++.

3)var num=Math.round(Math.random()*featopp.length-1)

There is nothing in your script named 'featopp'. And, for reasons I won't get into, that is not a good method to get a random number in the first place. Use this instead:

var num = Math.ceil(Math.random()* 5);

That will get you a random number that is either: 1, 2, 3, 4 or 5.

4)
Code:
```for (j=1; j<=5; j=j+1)
{
if (used[j-1] = num)```
When you are using loops with arrays, start at index 0, and then your loop counter will be the same as the array index:
Code:
```for (j=0; j<5; j++)
{
if (used[j] = num)```
When you get something working, I'll post a more efficient and easier to understand method.

8. Code:
```var featopp = new Array();
featopp[0] = new Array( "Sample Text 01", "Sample Text 01a" );
featopp[1] = new Array( "Sample Text 02", "Sample Text 02a" );
featopp[2] = new Array( "Sample Text 03", "Sample Text 03a" );
featopp[3] = new Array( "Sample Text 04", "Sample Text 04a" );
featopp[4] = new Array( "Sample Text 05", "Sample Text 05a" );

function featopp_write() {
var getNum = parseInt(rand(featopp.length) - 1);
document.write(featopp[getNum][0] + "<br>");
} else {
featopp_write();
}
}

var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
}
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}

little bit recursive but ok....
cheers

9. here is the latest version... IE props up an error saying that a script is taking to long to run and wants to end the script. so this is not an efficient way to do it.

and the reason i use featopp.length, instead of just 5 is because the length of the array may change, and i don't want to have to remember to change that number everytime i change how many items may be in the array.

and i am aware that I can write i=i+1 as ++1, but i have always written that way, and it works, so it is an old habit i don't find the need to struggle with. I'll fix that old habit after i give up caffiene.

Code:
```
<script language="JavaScript" type="text/JavaScript">

var featopp = new Array();
featopp[0] = new Array ( "Sample Text 01", "Sample Text 01a" );
featopp[1] = new Array ( "Sample Text 02", "Sample Text 02a" );
featopp[2] = new Array ( "Sample Text 03", "Sample Text 03a" );
featopp[3] = new Array ( "Sample Text 04", "Sample Text 04a" );
featopp[4] = new Array ( "Sample Text 05", "Sample Text 05a" );

var used = new Array(5);
var num = 0;

function featopp_write()
{ for (i=1; i<=5; i=i+1)
{
var num=Math.round(Math.random()*featopp.length)
used[i] = num
for (j=0; j<5; j=j+1)
{
if (used[j] = num)
{tryagain = 1}
}
if (tryagain = 1)
{i = i-1}
else
{
document.write(featopp[num][0] + "<BR>")
tryagain = 0
}
}
}

</script>```

10. Equality test is ==, not =

Code:
```...
if (used[j] == num)
{tryagain = 1}```
...
if (tryagain == 1)
...[/code]

11. Pretty complicated code for such a basic task.
Code:
```var featopp = new Array();
featopp[0] = new Array ( "Sample Text 01", "Sample Text 01a" );
featopp[1] = new Array ( "Sample Text 02", "Sample Text 02a" );
featopp[2] = new Array ( "Sample Text 03", "Sample Text 03a" );
featopp[3] = new Array ( "Sample Text 04", "Sample Text 04a" );
featopp[4] = new Array ( "Sample Text 05", "Sample Text 05a" );

function featopp_write() {
var total_out = 0

while (total_out < featopp.length) {
var feat = featopp[Math.floor(Math.random()*featopp.length)]
if (feat.out) continue
document.write(feat[0] + "<BR>")
feat.out = true, ++total_out
}
}

featopp_write()```

12. Not from me but from here
http://sedition.com/perl/javascript-fy.html

Fisher-Yates algo in JavaScript
Code:
```function fisherYates ( myArray ) {
var i = myArray.length;
if ( i == 0 ) return false;
while ( --i ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tempi = myArray[i];
var tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}```

13. i got Saturn's to work perfectly.. thanks man.. i knew i was over thinking things.

my question.. what exactly is feat.out
i'm not sure what that is doing...

Thanks to everyone else as well...

14. Originally Posted by Saturn
Pretty complicated code for such a basic task.
Code:
```var featopp = new Array();
featopp[0] = new Array ( "Sample Text 01", "Sample Text 01a" );
featopp[1] = new Array ( "Sample Text 02", "Sample Text 02a" );
featopp[2] = new Array ( "Sample Text 03", "Sample Text 03a" );
featopp[3] = new Array ( "Sample Text 04", "Sample Text 04a" );
featopp[4] = new Array ( "Sample Text 05", "Sample Text 05a" );

function featopp_write() {
var total_out = 0

while (total_out < featopp.length) {
var feat = featopp[Math.floor(Math.random()*featopp.length)]
if (feat.out) continue
document.write(feat[0] + "<BR>")
feat.out = true, ++total_out
}
}

featopp_write()```
It's not a basic task for someone without much programming experience. In any case, I guess it's all a matter of perspective: I would say your code is overly complicated as it's not something most people are going to understand. It also isn't as efficient as it could be: it's possible to write a script that only calls for a random number 4 times--in theory your script could actually get stuck in an infinite loop.

i got Saturn's to work perfectly.. thanks man.. i knew i was over thinking things.
Not really. It's natural to start with a brute force method, but then if you have enough experience, you can employ programming tricks to shorten the code and make it more efficient. But, if you don't struggle with the logic and come up with a working solution on your own, then you don't ever gain the experience.

15. Originally Posted by 7stud
Not really. It's natural to start with a brute force method, but then if you have enough experience, you can employ programming tricks to shorten the code and make it more efficient. But, if you don't struggle with the logic and come up with a working solution on your own, then you don't ever gain the experience.
well right now i'm not trying to gain the experience.. i'm just trying to get it done. writing javascript isn't something i do all the time.. which is why i have forgotten most of it. and when i'm done with this website, i'll go back to writing databases and such and in six months will have forgotten any javascript that I have learned.

the problem with my code is that i was getting stuck in a loop.. i think.. it was causing IE to crash anyway.. well it was causing it to come up with an error asking the user to stop running the script and if you said no, then it crashed.

maybe you could tell me how to stop that from happening in my code?
Thanks...

16. 1)You put the random number in an array
2)You loop through the array to see if the number is in there.
3)If the number is in there, you decrement the loop counter

Step 2) will always be true: if you put a number in an array, you are always going to find that number in the array.

17. Originally Posted by 7stud
1)You put the random number in an array
2)You loop through the array to see if the number is in there.
3)If the number is in there, you decrement the loop counter

Step 2) will always be true: if you put a number in an array, you are always going to find that number in the array.
well now... i feel a bit slow. I must have turned off the logic portion of my brain before writing the script. Now that I see it, I don't know how i let something like that slip by me. I know its been awhile, but that is just bad logic on my part.

So, then how can I test to see if a random number has already been generated, so that I can get 5 unique random numbers based on the number of options in the featopp array at the begining.. which is where the featopp.length came in.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•