Matlab question (possibly only a simple math problem)

  • Context: MATLAB 
  • Thread starter Thread starter sapiental
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on using the MATLAB function rand(1,1) to randomly select between three objects (A, B, C) for a computational physics program. The user proposes dividing the range of rand(1,1) output into three segments: Object_A for values [0.0000, 0.3333], Object_B for (0.3334, 0.6666), and Object_C for (0.6667, 0.9999). Feedback suggests that while the method is mathematically sound, it may introduce a slight bias towards Object_A due to the inclusion of 0 in its range. A proposed solution is to use Fix(10*rand(1,1)) to generate a number from 1 to 9, thereby ensuring equal distribution among the three objects.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with the rand function in MATLAB
  • Basic knowledge of probability and random number generation
  • Experience with conditional statements (if/else) in programming
NEXT STEPS
  • Explore MATLAB's rand function documentation for advanced usage
  • Learn about MATLAB's random number generation algorithms
  • Investigate methods to eliminate bias in random selections
  • Study the implications of pseudorandom versus true random number generation
USEFUL FOR

Students in computational physics, MATLAB programmers, and anyone interested in random number generation techniques and their applications in programming.

sapiental
Messages
110
Reaction score
0
Hello,

I'm currently writing a program for my computational physics course and ran into this issue with my program.

The function in MATLAB rand(1,1) gives you a random number from [0,1) to 4 significant figures. So for example if I ran this function it would look something like this.

input:
A = rand(1,1)
output:
A = .5040

Now I have to use this function to make it randomly choose beetween 3 objects (A,B,C). The problem I run into is that the professor told us to specifically use rand(1,1) for the random pick (Probably to test our math and get more familiar with the program.) Since this produces 10 possible values, then I need to assign (A,B,C) 10/3 apart.

Object_A = [.0000:.3333]
Object_B = (.3334:.6666)
Object_C = (.6667:.9999)

I set the object_pick = (rand(1,1))
possible values are (.0000-.9999)

then just write a bunch of if/else statements to determine which range of values the object_pick value falls.

Does this approach make sense to you guys from a mathematical point of view? Since the function rand(1,1) in MATLAB gives you a psuedorandom number as opposed to a truly randomly generated value as in C++, I feel that this method should be acceptable.

Any feedback, comments, or suggestions are much appreciated. Also, Thanks into whoever took the time to read this.
 
Physics news on Phys.org
Depending on how picky your prof is, the likelihood of object A coming up is *slightly* higher.
0 to .3333 includes 3334 different values (don't forget to count 0). Your other ranges have 3333 values.

You could probably do a quick if statement and ignore instances when the random number generator returns 0. Otherwise, it looks fine to me.
 
Yea, i can also do this. Fix(10*rand(1,1)) which will pick a number out of (0,1,2,3,4,5,6,7,8,9)

so excluding the 0 will give an equal distribution to 3 objects because it just tells the program to generate another number and doesn't really change anything. But, would the random pick still truly depend on rand(1,1)?

Thanks
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
25K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K