List of random numbers - arc4random - iphone

I want to create an array of numbers from 0-9 and want them to be randomized
Meaning, when a user clicks on a UIButton it creates an NSMutableArray of objects 4,5,8,3,6,2,9,1,7,0
When the user clicks on the button again it generates another list of 0-9 random numbers and so on.
The problem I have is with arc4random routine. That routine will spit out a random number between 0-9 one at a time. I gotta save that number it spits out and store it into an array. I will then check to see if the next random number it spits out is already in the array or not, if not then add it in the array otherwise keep looping till it finds a number that is not in my array. Keep doing this madness till my array size is 10.
Its all well and good for a small array of 0-9. What if I needed to create a random array of lets say between 0 - 1000.
What I am looking for is an efficient method that wont take 5 years to complete. Any thoughts?

As per this SO answer whats-the-best-way-to-shuffle-an-nsmutablearray, just create your list of numbers 0..9, (or 0..1000, whatever) in a mutable array and then randomly shuffle them.

You might want to use Random and Linq
Random random = new Random(0);
var myRandom = Enumerable.Repeat(0, n).Select(i => random.Next(0, 9));
where n is the amount of digits you want
Hope that helps

Related

Matlab function to create duplicate obs based on the value of a string variable

In my dataset, I have a variable which takes values like abc-ABC or abc-def-ABC (i.e., one or more lower-case codes and one upper-case code). I would like to (1) count the number of lower-case codes and capture this no. in a new variable (2) multiply the initial observation by this number (e.g., for abc-def-ABC, I would want 2 obs). Can anyone help?
I don't understand what you want with (2) but... To count the occurrences, can't you just
code = 'abc-def-ABC';
observations = numel(regexp(code, '[a-z]+-'))

Minizinc, counting occurrences in array of pairs

I'm new to constraint programming and toying around with some basic operations. I want to count the number of occurrences of an arbitrary element x in an array of pairs.
For instance, the following array has 2 eights, and 1 of every other element.
sampleArray = [{8,13}, {21,34}, {8,55}]
I wonder how I am to extract this information, possibly using built-in functions.
I'm not sure I understand exactly what you want to do here. Do you want to count only the first element in the pair?
Note that the example you show is an array of sets, not a 2 dimensional matrix. Extracting and count the first(?) element in each pair is probably easier if you have a two dimensional matrix (constructed with array2d).
In general there are at least two global constraints that you can use for this: "count" and perhaps also "global_cardinality". See http://www.minizinc.org/2.0/doc-lib/doc-globals-counting.html

assigning random number to an item and depending on that number the resultant number we get is store in some other variable

please could you help me to sort out one problem in matlab.
I have got a product i.e 100 items and i want to assign a random numbers between 1 and 3 to this item. depending on the number which i will get after assigning a random number to that item,i have to arrange the obtained number in some sort of array or another variable .
I try like this
item=1:100
R=randint(3,1,1)
shall i use some array or for loop so that i can solve this problem in matlab.
thanks
I am not sure if I understand you rightly, but I've reproduced your case like this:
s.item = 1:100;
s.range = randi(3, size(s.item));
s.number = arrayfun(#(x) randi(x, 1, 1), s.range);
Where, s.item is label of your item but you may not need this. I produce the range of random number and store it in s.range. Then I generate random value depending on range and store it in s.number. This may be an alternative to loops.
I hope this would be helpful for your case..

Extract a specific row from a combination matrix

Suppose I have 121 elements and want to get all combinations of 4 elements taken at a time, i.e. 121c4.
Since combnk(1:121, 4) takes a lot of time, I want to go for 2% of that combination by providing:
z = 1:50:length(121c4(:, 1))
For example: 1st row, 5th row, 100th row and so on, up to 121c4, picking only those rows from a 121c4 matrix without generating the complete combination (it's consuming too much for large numbers like 625c4).
If you haven't defined an ordering on the combinations, why not just use
randi(121,p,4)
where p is the number of combinations you want in your set ? With this approach you may, or may not, want to replace duplicates.
If you have defined an ordering on the combinations, tell us what it is.

Count the number of times a number is repeating in a vector

I have created a vector containing zeros and 1's using the following command in a for loop.
G(:,i)=rand(K,1)<rand;
Since this is part of a larger problem at a particular stage I need to count the number of 1's that are present in each column.
I have tried to find the count using a for loop which is very messy and takes too long.
I found that histc can be used for this but I get an error
histc(G(:,1),1)
First input must be non-sparse numeric array.
Is there a better way to do this or am I missing something here ?
If you have a matrix G containing zeroes and ones, and you want to know how many ones are in each column, all you need is SUM:
nZeroes = sum(G);
This will give you a vector containing a total for each column in G.