C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 23rd, 2005, 12:30 PM
Packetloss Packetloss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 1 Packetloss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 57 sec
Reputation Power: 0
Random Number with Range

Hi,

How would I create a random number within a certain range like 0-10, but also include the decimal values as well? Such as 1.1, 3.2. 5.7, etc.

Thanks

Reply With Quote
  #2  
Old June 23rd, 2005, 02:30 PM
Ashaman0 Ashaman0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 1 Ashaman0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 10 sec
Reputation Power: 0
Just get a random number between 1 and 100, then divide it by 10. That will give you a random number between 1 and 10 including decimals out to the tenths place.

Reply With Quote
  #3  
Old June 23rd, 2005, 03:40 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Actually, you'll need to mod the random number by the maximum number, it gives better range

Just generate a random number between 1 and 100, then multiply it by .1, thus if you generated 11, it would become 1.1, if it generated 32 it would become 3.2, etc....
Comments on this post
netytan agrees: I like it .
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #4  
Old June 30th, 2005, 02:38 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
Remember that if you need a true random number, srand is NOT going to give it to you,

Reply With Quote
  #5  
Old July 8th, 2005, 07:12 PM
nishivision nishivision is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 12 nishivision User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 58 sec
Reputation Power: 0
there is a random number generator function in c library.
if you want to generate up to 10 random numbers then simple write rand() % 11 and if you want to generate from 0 to 10 then the syntax for this is rand() % ((max +1) - min)) + min

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Random Number with Range


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT