User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,789 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1626 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 63
Reputation: knowledgelover is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
knowledgelover knowledgelover is offline Offline
Junior Poster in Training

Question Re: C++ Random Numbers

  #1  
Nov 30th, 2007
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 removed
Last edited by Narue : Nov 30th, 2007 at 5:16 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,284
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 174
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: Negative Random Numbers

  #2  
Nov 30th, 2007
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 63
Reputation: knowledgelover is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
knowledgelover knowledgelover is offline Offline
Junior Poster in Training

Re: Negative Random Numbers

  #3  
Nov 30th, 2007
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
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: Negative Random Numbers

  #4  
Nov 30th, 2007
If your problem has been solved...please mark this thread as being solved! Thx
I shot the sheriff....but I didn't shoot the deputy
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 744
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Negative Random Numbers

  #5  
Nov 30th, 2007
you could however generate positive values and multiply by -1
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC