DEV Community

loading...

Discussion on: 3 Amazing ways to generate random numbers without Math.random()

Collapse
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻 Author

Umm. No! computers(CPUs) do not handle bit shifts like you mentioned. Rather it takes place entirely in Binary in the CPU registers. The instruction 42*(2**5) consumes more resources of the CPU than 42<<5

Collapse
wikunia profile image
Ole Kröger

It produces the same result that's all I said. Nevertheless in C++ it's compiled to the same (42*32) and 42 << 5 both to the latter because bit shifting is faster.

Thread Thread
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻 Author

I think it is better to be explicit in this case.😎
Without knowing the target system, using multiplication is a risk.🙁

Thread Thread
wikunia profile image
Ole Kröger

I didn't want that it uses multiplication. I thought it would be nice to mention that the result (mathematically) is the same instead of: it's changing the result to make a completely new number. Make it clear to the reader what this does and how to visualize it. Then people who don't know it can use it if they want to multiply by a power of two somewhere.

Thread Thread
svijaykoushik profile image
Vijay Koushik, S. 👨🏽‍💻 Author

Oh... Okay 👍🏽. You're right. Why I didn't do is because,I thought it would be confusing to the readers.

Thread Thread
wikunia profile image
Ole Kröger

Fair enough. Maybe an extra box for extra information but understand your point.