Anyone Here Know Coding?

Kinja'd!!! "Sportwägen, Driver Of The Red Sportwagen" (sportwagen)
10/01/2014 at 22:14 • Filed to: HALP

Kinja'd!!!0 Kinja'd!!! 10

Specifically Python... I can't find any useful resource to tell me how to randomize a list within a "while" loop. I can haz halp?

EDIT: I figured it out! (With some help from KusabiSensei ;)


DISCUSSION (10)


Kinja'd!!! KusabiSensei - Captain of the Toronto Maple Leafs > Sportwägen, Driver Of The Red Sportwagen
10/01/2014 at 22:19

Kinja'd!!!0

random.shuffle(x) ?

EDIT: Specifically, assume array x. Is there some reason you can't have it call random.shuffle(x) upon each iteration of the loop?


Kinja'd!!! Sportwägen, Driver Of The Red Sportwagen > KusabiSensei - Captain of the Toronto Maple Leafs
10/01/2014 at 22:20

Kinja'd!!!0

Where would you put it though?


Kinja'd!!! roflcopter > KusabiSensei - Captain of the Toronto Maple Leafs
10/01/2014 at 22:25

Kinja'd!!!0

This method wouldn't actually need to use a loop to randomize it though... it would just shuffle the contents of the array and call it a day, correct? I believe he is working on an assignment that would require the use of a loop to do it, in which case I would say you should copy the contents into another array and then copy each element back into the original one at a time to a 'random' index until all the available indexes are used.

I could be completely off my rocker, I've been programming in nothing but assembly lately and seem to think of the most complicated ways to do everything.


Kinja'd!!! KusabiSensei - Captain of the Toronto Maple Leafs > Sportwägen, Driver Of The Red Sportwagen
10/01/2014 at 22:26

Kinja'd!!!0

x = range(10)
while var != condition:
random.shuffle(x)
#Do Other Stuff

print "Done"


Kinja'd!!! Sportwägen, Driver Of The Red Sportwagen > KusabiSensei - Captain of the Toronto Maple Leafs
10/01/2014 at 22:27

Kinja'd!!!0

LOL thanks!


Kinja'd!!! deekster_caddy > Sportwägen, Driver Of The Red Sportwagen
10/01/2014 at 22:28

Kinja'd!!!1

Ooh, ooh, I know Python, and also how to randomize a loop! See the video below for proof!

Okay, I am actually only a teensy eensy bit familiar with the Python YOU need, and not enough to be of service. But I would assume you want to put that random.shuffle(x) thing mentioned elsewhere inside the loop. Unless you need it to not be completely random, then you need to include a reference to the list you are building so it doesn't repeat something already in the list... but I have no idea what you are up to.


Kinja'd!!! KusabiSensei - Captain of the Toronto Maple Leafs > roflcopter
10/01/2014 at 22:28

Kinja'd!!!0

I could be as well. I've already been drinking.

I literally randomized the array inside the while loop, not used the while loop as the control structure to randomize the array.


Kinja'd!!! KusabiSensei - Captain of the Toronto Maple Leafs > Sportwägen, Driver Of The Red Sportwagen
10/01/2014 at 22:29

Kinja'd!!!0

roflcopter also makes a good point.

Is your goal to use a while loop to iterate over the array and shuffle the contents, or do you want to shuffle the contents of a given array upon each iteration of a while loop?


Kinja'd!!! roflcopter > KusabiSensei - Captain of the Toronto Maple Leafs
10/01/2014 at 22:31

Kinja'd!!!0

I've literally never even looked into anything in python, so.... I'm assuming random.shuffle() is a standard function in python? If so, that's pretty nifty and I'd definitely say that's the best way to do it, unless you want something more random than the random function can give. Hooray for drinking!


Kinja'd!!! Sportwägen, Driver Of The Red Sportwagen > deekster_caddy
10/01/2014 at 22:33

Kinja'd!!!1

*SIGH* Funny! I figured it out though :)