LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-03-2006, 07:00 PM   #1
jacques83
Member
 
Registered: Nov 2005
Posts: 34

Rep: Reputation: 15
Random number generation


I want to generate random numbers from 0 to 20 in my program, is there any function in Linux which I can use?

Thanks
 
Old 02-03-2006, 07:14 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Here's a link to generate random integers within a bounds: http://www.linux.com/guides/abs-guide/randomvar.shtml
 
Old 02-04-2006, 01:09 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. If you're programming in C/C++:

man 3 rand

2. If you're on Linux (regardless of language), consider reading from /dev/urandom. This will give you truly random numbers.

3. Regardless of how you generate the random (or pseudo-random) number, you'll usually constrain it to a specific range with the "modulus" operator

C/C++ EXAMPLES:
a) inum = rand () % 20
<= returns a number between 0 .. 19
b) inum = rand () % 21
<= Returns a number between 0 .. 20
c) inum = (rand () % 20) + 1
<= Returns a number between 1 .. 20
 
Old 02-04-2006, 01:12 AM   #4
jacques83
Member
 
Registered: Nov 2005
Posts: 34

Original Poster
Rep: Reputation: 15
I want to generate Random nos, and not pseudo-random, in that case, i cannot use rand right?
 
Old 02-04-2006, 08:39 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Can you define what you really require?
For most tasks pseudo-random numbers are sufficient. Why do you need true random numbers? It can be done but its isn't easy.
What language are you going to use for this?
 
Old 02-04-2006, 12:38 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Reading /dev/random /dev/urandom gives you random bytes, using "noise" from device interrupts etc, hence close-to-true-random bytes. Using /dev/random your program may have to wait until sufficient "noise" is gatherd by the kernel, while /dev/urandom will use the noise if available, but will mever block if there's not sufficient "noise".

See:
man 4 random
or the online man page: http://linux.about.com/od/commands/l/blcmdl4_random.htm
 
  


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- static messing with random number generation titanium_geek Programming 8 10-15-2005 01:39 PM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM
Random Number Game tearinox Programming 4 08-20-2004 10:12 PM
Generation of random primes SaTaN Programming 12 10-30-2003 04:30 PM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM

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

All times are GMT -5. The time now is 04:05 PM.

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