BSD Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsBSD Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 15th, 2011, 03:36 AM
ktoz ktoz is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Posts: 562 ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 3 h 4 m 2 sec
Reputation Power: 69
Using random() function for limited ranges

I know this is an easy one, but the Mac man pages are completely devoid of examples and 1/2 hour of Googling produced not a single useful example of how to use the output from the random() function and clip it to specific number ranges. Every site mentioning it gets all carried away with the mathematics behind how it works and forgets to mention how mere mortals can use it to produce numbers between 0 and 26 or 0 and 100, or 0 and 1000 etc. How do you do it?

Thanks for any help.

Reply With Quote
  #2  
Old June 16th, 2011, 05:05 AM
ktoz ktoz is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Posts: 562 ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level)ktoz User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 3 h 4 m 2 sec
Reputation Power: 69
[Solved]

After playing around with various combinations, it did indeed turn out to be easy. For others who might need the solution, just do the following:

To get numbers between 0 and 25
random() % 25;

To get numbers between 0 and 100
random() % 100;

To get numbers between 0 and 1000
random() % 1000;

etc...

Reply With Quote
  #3  
Old May 8th, 2012, 08:20 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383
Your answer is incorrect. Look here--->
Suppose your maximum random number is 3.
You have a uniform distribution amongst 0, 1, 2, 3.

Now, we want a random number uniformly distributed from 0, 1, 2.

random() % 3 will generate numbers with probability
Code:
number  probability
0         1/2
1         1/4
2         1/4


Yes, so what is the best solution.
see thread
http://forums.devshed.com/c-programming-42/rand-code-to-simulate-rand-max-895833.html

Make sure that RAND_MAX+1 converted to double is different from RAND_MAX otherwise use extra logic to remove the case when RAND_MAX == random() .
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsBSD Help > Using random() function for limited ranges

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap