Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# How to generate a Random no with in a Range

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

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month12 days, 5 hours left to enroll