What purpose exactly does scaling / shifting rand values serve? The book I'm learning from really doesn't seem to explain what these actions do.


-Clifton

Recommended Answers

All 3 Replies

>What purpose exactly does scaling / shifting rand values serve?
rand gives you a number from 0 to some big value. Sometimes you don't want that. Sometimes you want a number from 0 to 10. But if you might get a number from 0 to 32767, you need to force that value into your desired range. That's what the scaling and shifting techniques do.

Alright, I just wanted to make sure I was understanding it alright.

If I want to specify a range, it would be like:

num = 2 + rand() % 10;

for the range of 2-10. But how would I do it if I wanted only even numbers within that range? Or every fourth number?

Is there even a way to do that, or can you only specify a range and not specifics within that range?

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.