?
Solved

random number generation

Posted on 2003-02-21
7
Medium Priority
?
277 Views
Last Modified: 2010-04-17
Hi,
Can I please get a C code for generating  a random number that is uniformly distributed in the range[8,32].
Thanks.
0
Comment
Question by:bujji71
[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
7 Comments
 
LVL 1

Accepted Solution

by:
Gula earned 200 total points
ID: 7994485
if I understood right, then

rand()%25 + 8;
0
 

Expert Comment

by:luisseixas
ID: 7994568
I don't know about C but, check this Delphi code out. Maybe you can get some ideas.

First, somewhere in your project, you start then built-in random number generator:


procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;


The "Random" function gives you a number 0 <= N < 1. Just do something like this:


procedure TForm1.ButtonClick(Sender: TObject);
const Upper=32;
      Lower=8;
var   Number: Integer;
begin
  Number:=Trunc(Random*(Upper-Lower+1))+Lower;
end;


Easy!
0
 

Expert Comment

by:Leo71
ID: 7995194
For Windows, MS VC I would suggest:

#include <stdlib.h>
#include <time.h>

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

   (rand()*(32-8))/RAND_MAX+8;

srand with time is because otherwise you programm starts everytime with the same sequence of random numbers.

If you search for a code to implement you own random numbe generator I can't help you.

Regards
Leo
0
Independent Software Vendors: 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!

 

Expert Comment

by:d34thk
ID: 8006999
mmhmm...

use

srand(time(NULL));
rand()%x+y;

i cant remember how the x and y works, but if u work it out, it makes the random number be between them.

i think the minimum random number is x, while the maximum is x+y (add them together)

so 1+2 would give u a random number between 1 and 3

correct me if im wrong
0
 

Expert Comment

by:Leo71
ID: 8009592
you mean srand()%(32-8 +1)+8, I guess :), like Gula said.

With other words: srand()%(Range+1)+startvalue
which works as long fine, as Range < RAND_MAX.

My solution ((rand()*Range)/RAND_MAX+startvalue can blow up the values over RAND_MAX, but the resolution still sticks at RAND_MAX.
But one could multiply two random numbers, if a higher range+resolution is needed.... ;-).

Regards
Leo
0
 

Expert Comment

by:CleanupPing
ID: 9447588
bujji71:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

770 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