LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2013, 08:37 AM   #1
Richa Bansal
LQ Newbie
 
Registered: Jan 2013
Posts: 1

Rep: Reputation: Disabled
Question How to create random integers in a given range in c programming


How to create random integers in a given range in c programming.
Thanks

Last edited by Richa Bansal; 01-24-2013 at 12:49 AM.
 
Old 01-23-2013, 08:39 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
https://www.google.co.uk/search?q=Ho...+c+programming

You migth want to say please / thank you in future when asking for help, and state more than a title in your post.

Last edited by acid_kewpie; 01-23-2013 at 08:43 AM.
 
Old 01-23-2013, 09:35 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
man 3 rand
man 3 random

Use a multiplier to scale the result.

--- rod.
 
1 members found this post helpful.
Old 01-23-2013, 11:30 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Another question would be: how random ?

Unseeded rand will give you the same number every time. How do you plan on seeding it, and how random do the numbers need to be ?
 
Old 01-26-2013, 08:06 AM   #5
Tryptophan
LQ Newbie
 
Registered: Jan 2013
Location: Buenos Aires, Argentina
Distribution: Slackware
Posts: 3

Rep: Reputation: Disabled
Well, you can simulate a random number... If you're able to use a function which gives you the amount of seconds of a time period, you can take that last number, knowing that will oscillate in a big way, and use it as a random.
 
Old 02-12-2013, 12:55 AM   #6
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
Try this :
...
int num;
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
num = rand ();
...
 
Old 02-12-2013, 01:51 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Be careful about seeding the PRNG ... most of the time, the runtime library has already done this, and you don't want to re-seed unless you need a repeatable sequence.

There are, generally, two flavors of PRNG (= Pseudo Random-Number Generator) functions. One flavor produces a known-positive integer.   Simply use the modulo operator to clip that to the desired range. Another flavor produces a floating-point number in the range [0..1]. Simply multiply this value, and truncate to an integer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java random from a range manolakis Programming 1 10-29-2007 03:24 PM
socket programming function to read/write integers? ashlesha Linux - Networking 4 04-25-2007 10:45 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 05:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration