Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Difficulty in randomization

by kejohm (Hermit)
on Mar 04, 2012 at 21:41 UTC ( #957816=note: print w/ replies, xml ) Need Help??


in reply to Difficulty in randomization

Instead of using if in your loop, you can use a do {...} while loop, eg:

my @array; for ( 1 .. 10 ) { my $random_number; do { $random_number = rand 600; } while $array[$random_number] eq 'X'; $array[$random_number] = 'X'; }

Here we generate a random number inside the loop and check whether the element at that position has been assigned 'X'. If it is, we try again. Otherwise, we assign 'X' to the selected element.


Comment on Re: Difficulty in randomization
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://957816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2015-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who makes your decisions?







    Results (364 votes), past polls