Solved

# How to generate a Random no with in a Range

Posted on 2003-02-27
Medium Priority
279 Views
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

0
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

Expert Comment

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

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

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

ID: 8075145
I must say you are a C wizard
0

## Featured Post

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