?
Solved

How to generate a Random no with in a Range

Posted on 2003-02-27
4
Medium Priority
?
279 Views
Last Modified: 2010-04-15
Hi
Since I am a novice in this area and I donot know much about the random generation concept Can anyone pls let me know by any sample whether there is any way by which we can generate a random number with in a given specified range

Thankx in advance
0
Comment
Question by:gaurav_chikara
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:posternb
ID: 8036075
The standard way of generating 'random' numbers in C is the rand() function.  This function returns any random number so to get it within a certain range take the modulus of it. i.e.

rand()%10 returns numbers between 0-9, inclusive

I quoted 'random' above because the numbers aren't truly random, in fact you'll get the same string of numbers for any given seed.  You seed the generator by calling srand( seed ).  A popular technique is to seed with the current time, so you'll get a new sequence everytime you run the program (at a different time of course).

srand( time(0) );
0
 
LVL 6

Accepted Solution

by:
gj62 earned 400 total points
ID: 8036522
A few clarifications:

1) rand() won't return any random number.  In stdlib.h there is a RAND_MAX const - in VC++, that is 0x7fff, or 32,767.

2) You should seed the random number generator using srand( time(0) ) only once - I've seen many programs that do the following:

for()
{
  srand( time(0) );
  someInt = rand() %100;
}

and they get the same random numbers in a row for awhile, then it changes and they are the same again, etc.  What's happening is that time returns seconds since midnight on Jan 1 1970 - your seeding your generator with the same number until the next second (which is a long time in terms of the execution of the above loop!).

Anyhow, just make sure srand() is only called once in your program.
0
 

Expert Comment

by:pn206
ID: 8040493
Random number generation is actually a very complicated task. The inbuilt C functions (rand(), srand(), etc) are notorious for generating poor random numbers. The book "Numerical Recipes in C" has lots on information on generating "good" random numbers with source code. Check out chapter 7 at:

http://www.library.cornell.edu/nr/bookcpdf.html

0
 

Author Comment

by:gaurav_chikara
ID: 8075145
Your solution really worked
I must say you are a C wizard
thankx for ur answer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question