LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack 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
Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Squeeze (x86)
Posts: 6,092

Rep: Reputation: 264Reputation: 264Reputation: 264
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
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
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,352

Rep: Reputation: 129Reputation: 129
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: ubuntu
Posts: 2,524

Rep: Reputation: 93
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 05:07 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration