Message Boards Message Boards

0
|
9183 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

I need a way to randomly change a few elements in a list

Posted 9 years ago

Suppose you have 4 arrays of different number of rows, but same number of columns.

I need a way to generate a list of 0s and 1, which I can do with the following

 lq= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Table[ReplacePart[lq, RandomInteger[{1, 10}] -> 1], {i, 10}]

Now let's say you have a different list where you have to replace 3 random choices of 0s with 1s. And they have to be all different elements, meaning I need 3 0s replaced

how do I accomplish that?

lw={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0}

The following didn't work.

Table[RandomInteger[{1, 49}] /; %[[1]] != %[[2]] != %[[3]], {10}]

{13 /; %[[1]] != %[[2]] != %[[3]], 49 /; %[[1]] != %[[2]] != %[[3]], 
 25 /; %[[1]] != %[[2]] != %[[3]], 6 /; %[[1]] != %[[2]] != %[[3]], 
 39 /; %[[1]] != %[[2]] != %[[3]], 27 /; %[[1]] != %[[2]] != %[[3]], 
 48 /; %[[1]] != %[[2]] != %[[3]], 21 /; %[[1]] != %[[2]] != %[[3]], 
 20 /; %[[1]] != %[[2]] != %[[3]], 23 /; %[[1]] != %[[2]] != %[[3]]}

Thanks you any help

edit. The first list was supposed to be 10 elements long. Not 23 like I posted. ut that doesn't really matter.

POSTED BY: bored dude
4 Replies
Posted 9 years ago

What are you trying to achieve? If you are trying to generate pseudorandom permutations, the following code does this directly:

In[1]:= RandomSample[{1, 1, 1, 0, 0, 0, 0, 0}]

Out[1]= {0, 1, 1, 0, 0, 0, 0, 1}

In[2]:= Table[RandomSample[{1, 1, 1, 0, 0, 0, 0, 0}], {5}]

Out[2]= {{0, 1, 0, 0, 0, 1, 0, 1}, {1, 1, 0, 0, 0, 1, 0, 0}, {0, 1, 0,
   1, 0, 1, 0, 0}, {1, 1, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 1}}
POSTED BY: David Keith
Posted 9 years ago

I'm trying to generate a list containing the 0s and 1 or 1s to dot product with various columns of data. (2 columns actually, ffpg points and salary for fantasy football)

By mutiplying the zeros, i can null out the all the values except the chosen ones that are multiplied by 1s. Then i need to feed that function into the maximize function in mathematica.

Once maximized, I can use that list of 1s and 0s to figure out which players were chosen.

Thank you for the help.

POSTED BY: bored dude
Posted 9 years ago

Hi. Don't know if this would be of value. This is a smaller size for posting... Here are all the lists with just 2 1's out of 5

v={1,1,0,0,0};

Permutations[v]
{{1,1,0,0,0},{1,0,1,0,0},{1,0,0,1,0},{1,0,0,0,1},{0,1,1,0,0},{0,1,0,1,0},{0,1,0,0,1},{0,0,1,1,0},{0,0,1,0,1},{0,0,0,1,1}}
POSTED BY: Dana DeLouis

To generate a random list of 0s and 1s:

In[1]:= RandomChoice[{0, 1}, 10]

Out[1]= {0, 0, 1, 0, 0, 1, 1, 0, 1, 0}

To randomly change a couple elements in list lw:

randomChange[list_, num_] := 
 ReplacePart[
  list, # -> RandomChoice[{0, 1}] & /@ 
   RandomSample[Range[Length[list]], num]]

randomChange[lw, 3]

For future reference, you can generate an arbitrary length of constant values with ConstantArray.

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract