MATLAB - random allocation of random number

In summary, the person is looking for a way to generate a random number between 0 and 1 and allocate it to one of 100 groups, repeating this process a million times. The suggested solution is to use a for loop with a pre-allocated array and randomly generate positions within the array to place the variable, making sure not to repeat any allocations.
  • #1
mathman44
207
0
Hi. I'm being asked to generate a random number either 0 or 1 and then to randomly allocate this 0 or 1 to one of 100 groups, and then to repeat this many (a million) times.

So... I can easily generate randomly either a 0 or 1 by randint(1), but how do I randomly allocate this to 1 of 100 "groups" (one row of a column vector, for example)? And then how do I repeat this process a million times?

Any help would be greatly appreciated.
 
Last edited:
Physics news on Phys.org
  • #2
What you can do is use a for loop with a pre-allocated array. Then you would have to randomly generate positions within the array to place your variable. You would have to place your upper bound as 100 and careful not to repeat any allocations.
 

1. What is the purpose of using random allocation of random numbers in MATLAB?

The purpose of using random allocation of random numbers in MATLAB is to generate a set of numbers with no discernible pattern or order. This is useful for simulating real-life scenarios, testing algorithms, and conducting statistical analyses.

2. How can I generate a random number in MATLAB?

To generate a random number in MATLAB, you can use the rand function. This function returns a single uniformly distributed random number between 0 and 1.

3. Can I specify the range of random numbers to be generated in MATLAB?

Yes, you can specify the range of random numbers to be generated in MATLAB using the randi function. This function allows you to specify the minimum and maximum values for the random numbers to be generated.

4. How can I generate a matrix of random numbers in MATLAB?

To generate a matrix of random numbers in MATLAB, you can use the rand function with the desired number of rows and columns as inputs. This will return a matrix with uniformly distributed random numbers between 0 and 1.

5. Is it possible to generate reproducible random numbers in MATLAB?

Yes, it is possible to generate reproducible random numbers in MATLAB by setting a seed value using the rng function. This will ensure that the same set of random numbers is generated every time the code is run, making the results reproducible.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
15
Views
1K
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
591
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
887
  • Engineering and Comp Sci Homework Help
Replies
23
Views
3K
Back
Top