hi,
Thanks a lot for posting us this thread it is really valuable
But I wonder if I can make the range including negative values besides positive numbers
e.g: from -1 to 1

if you can help be I'll appreciate it
thanks again


knowledgelover

[email]email removed[/email]

Recommended Answers

All 4 Replies

The range from -1 to 1 is 2. So get random numbers from 0-2 (rand() % 3 should do that), then subtract 1 from that number. The results should be -1, 0 or 1. Now if you want decimal values, not integer values, ranging from -1 to 1, then a slightly different approach would be needed.

oh thanks again
yes I need it to be for decimal and I discovered the approach
it is like this
(2*static_cast<double>(rand())/static_cast<double>(RAND_MAX))-1
right?
it worked for me ;)

thanks again

If your problem has been solved...please mark this thread as being solved! Thx

commented: Why do you insist on posting inane crap? Instead of spamming, why don't you help someone for a change? +0

you could however generate positive values and multiply by -1 :)

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.