How do I create a random double generator where only multiples of .05 are used?
    2 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Giann Geocadin
 el 27 de Ag. de 2014
  
    
    
    
    
    Respondida: Geoff Hayes
      
      
 el 27 de Ag. de 2014
            I am trying to randomly generate a double, however it needs to be within .25 and .7 and it has to be divisible by .05.
0 comentarios
Respuesta aceptada
  Geoff Hayes
      
      
 el 27 de Ag. de 2014
        Giann - you could create an array of all the numbers within the interval [0.25, 0.7] that are multiples of 0.5, and then just randomly pick one.
 % create an array of choices, where each is a multiple of 0.05
 choices = [25:5:70]/100.0;
 % randomly pick one using *randi*
 randomDbl = choices(randi([1 length(choices)],1,1));
If we take your original interval and multiply it by 100, then we get [25,70]. Then we can create the array of choices which is every integer from 25 to 70 using a step size of 5. When we divide the result by 100, we have all doubles within the interval [0.25,0.7] that are multiples of 0.05.
Now we use randi to randomly choose an index between 1 and the number of choices, length(choices), and use that index to pick our "random" double.
Try the above and see what happens!
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Random Number Generation en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!