-2

I generate a number in the range 1 to 100. This number should be in the first coordinate of the pos = vec(0,0,0) (so on the x-axis). The other axes should stay 0. How can I transfer the generated number to this point?

from vpython import*
from random import randint



#Sonne
sun = sphere(pos = vec(0,0,0), radius = 9, make_trail = True ) 
sun.mass = 2e30   
sun.velocity = vec(0,0,0)

#Merkur
mercury_pos_x = randint(1, 100)
mercury = sphere(pos = vec(mercury_pos_x,0,0), radius = 5, color=color.red, make_trail = True ) 
mercury.mass = 3.25e23
mercury.velocity = vec(0,0,-47000)

#Venus
venus_pos_x = randint(1, 100)
venus = sphere(pos = vec(venus_pos_x,0,0), radius = 6, color=color.cyan, make_trail = True ) 
venus.mass = 4.9e24
venus.velocity = vec(0,0,-35000)
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ragnarök
  • 138
  • 10
  • Do you want *one* random value used for each of the bodies, or a separate value for each body? Does the Sun remain at (0,0,0)? – Prune Sep 27 '18 at 22:56
  • @Prune a separate. The sun yes. But I actually would have 9 planets and the sun. This is just an excerpt. So one value for every planet on the x axis – Ragnarök Sep 27 '18 at 22:58
  • Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – Machavity Oct 15 '18 at 20:00

1 Answers1

1

I suspect that you want a different random start for each planet, leaving the Sun at (0,0,0). For two random numbers, you need to call random twice, keeping the value each time for later use.

merc_pos = randint(1, 100)
mercury = sphere(pos = vec(merc_pos,0,0), radius = s_rad1/2,
                 color=color.red, make_trail = True ) 

venus_pos = randint(1, 100)
venus = sphere(pos = vec(venus_pos,0,0), radius = s_rad1/1.8,
               color=color.cyan, make_trail = True ) 

If you're not going to use the random value elsewhere, you can plug it in place:

mercury = sphere(pos = vec(randint(1, 100),0,0), radius = s_rad1/2,
                 color=color.red, make_trail = True ) 
Prune
  • 76,765
  • 14
  • 60
  • 81
  • thank you I was looking for smth like that. The only problem I have now is that after 0.5 seconds the simulation is breaking down and the window just quits(when debugging) When I start without debugging it says "ImportError: cannot import name 'randint'". should I import another package? I renewed the code in the question with your solution. – Ragnarök Sep 27 '18 at 23:13
  • Those are unrelated problems. You should continue to import `randint` just as you always have. If you can't manage to find the problem with incremental changes to your original code and on-line searches, then you should post a separate question. That question would include the code to reproduce the problem. Since neither of those conditions appears here, I can't really help you any more. – Prune Sep 27 '18 at 23:27