Using Rnd with specific numbers rather than a range - basic4android

Is it possible to generate a random integer from a list of numbers or predefined set - that may not be in order.
For example - generate a random number from 1,2,4,5 (no 3 allowed).

You can create an array with all your predefined set into it, then you fill an integer index with a random number from 0 to the array's length. That should do the trick :)

Related

Designing a hash function that creates keys for a hash table from an alphanumeric number

I am trying to design a hash function using customer IDs that range from AA0001 to ZZ9999.
The keys will be stored in a one dimensional array.
Each element of the array will need to be accessed.
My thinking is that I can sum the ascii values of the customer ids as well as the following numbers.
I am planning to have an array size of 100.
I am new to this subject so not clear whether my thinking is correct.
The smallest number is AA0001 conversion of Ascii of AA is 130 and 1 makes smallest limit to be 131.
Maximum number ZZ9999 is 180 + 9999 = 10179.
I am want to use modulus function but not sure how to use this function to give me a range of numbers between 1 to 100.

How do I save the indices of the top 4 maximum numbers in a matrix in scilab

I need to save the indices of 4 maximum numbers
for example, I need to get the indices of rows 10,9,7,5
5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878
I have used
[mA,nA]=max(distA)
where mA is the row and nA is the column
in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.
You can use the gsort function:
[S,ind]=gsort(distA,"g","d");
The index of the 4 largest elements is the given by
ind(1:4)
You can use this little trick.
[output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.

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]+-'))

List of random numbers - arc4random

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

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.