I'm making a game in Python, where two armies battle each other. The soldiers both spray arrows at each other. But for each soldier, there is a 1 in 15,000 chance every frame them firing. One thing I noticed was the FPS dropped from 60 to less than 3. Both armies combined have about 5,000 soldiers, so that means about 75,000,000 random numbers are generated every frame. Is there a way I can generate huge amounts of random numbers efficiently? I was using the randrange
function, which is pretty slow. I plan to expand the armies, but it seems hopeless.
Ethan_145 0
Newbie Poster
Recommended Answers
Jump to PostWith 5000 soldiers that should be 5000 random numbers. The 1/15000 chance requires just one random number.
Jump to PostThat sounds like the perfect scenario to create a thread that generates the random numbers. That would allow the generation to be done concurrently with your game. The thread could maintain a queue of n random numbers, automatically generating new numbers to maintain the pool as you make pull requests. …
Jump to Post""" Name: Thread.py Description: Simple (I hope) example of how to run a piece of code in a separate thread. This code is the result of a question from a user who needed to generate many random numbers, but found it was slowing his application down. Random_Pool …
Jump to PostTime for a reality check.
Generating a pseudo random number is one multiplication, one addition, one modulo.
It’s a fast as a function can be.
You can’t make it faster by adding the overhead of storing and retrieving values. The use of sleep to synchronise around an empty pool will …
Jump to PostFlip the equation. 2,500 soldiers have a 1 in 15K chance to fire per frame. or 2500 in 15000. To get to 15000 in 15000, that's 6 frames for a random arrow. Or 1/10th of a second per arrow.
I only need one random number every 1/10th of a second …
All 20 Replies
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
pritaeas 2,194
¯\_(ツ)_/¯ Moderator Featured Poster
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
Ethan_145 0
Newbie Poster
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
rproffitt 2,572
"Nothing to see here." Moderator
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
Ethan_145 0
Newbie Poster
Dani 4,084
The Queen of DaniWeb Administrator Featured Poster Premium Member
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
Ethan_145 0
Newbie Poster
Reverend Jim 4,780
Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
toneewa 81
Junior Poster in Training
JamesCherrill
commented:
Yes, benchmark to identify performance problems, don’t guess +15
JamesCherrill 4,733
Most Valuable Poster Team Colleague Featured Poster
Ethan_145 0
Newbie Poster
toneewa 81
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.