Random number generation


 
Thread Tools Search this Thread
Top Forums Programming Random number generation
# 1  
Old 02-13-2006
Random number generation

Hi...How can I generate random numbers between a given range ...preferably between 1 and 100...in UNIX C programming...?

I tried out functions like rand(),drand48() etc but didnt get satisfactory results...

Thanks a lot in advance..........
# 2  
Old 02-13-2006
Would you please explain what do you mean by unsatisfactory? I suppose most applications generate random numbers with these functions for cross-platform compatibility as they are standard.

In the rand() manpage, somewhere in the bottom, there is a note that discusses how to use that properly. Did you use it like that?

http://linux.com.hk/penguin/man/3/ra...on=3&name=rand

On some systems there are other means for random number generation. For instance, on Linux PCs the /dev/hwrandom represents a hardware random number generator . But this is only present if this support is compiled and available in the kernel. You may also try /dev/random and /dev/urandom.

http://linux.com.hk:80/penguin/man/4/random.html

Code:
cbkihong:~# cat /dev/random | hexdump 
0000000 1169 ce30 3785 5b36 7afe 84ff e6f6 e637
0000010 ea63 90b5 1f59 bf52 4296 b4c1 c129 5416
0000020 2940 608e 5fed e724 fd0e 4f92 3d75 4de5
0000030 1c6f 4d22 80bf fedf a208 ff41 5163 c146
0000040 838e 9ab2 fa6d 5b51 0715 ea56 d630 99dd
0000050 6b70 2ff3 11f6 feb6 3e17 b241 e5b9 dafb
0000060 e2fd 78cd c1d2 ed64 970c e5f4 fcf1 0fad
0000070 55c0 b395 629d a43d abc1 5b1d ad28 515f
0000080 72d0 8d68 bd22 5d19 171e 4f6b d28a 9bf2
0000090 f7a0 016a 8ba9 be22 8473 34ee 3f67 3c7c
00000a0 a5ce a23c 3b36 2463 cc94 99b2 fc91 0991

Here is to show you the output looks random.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Random password generation

Hello, I have created a script to generate a random password on Linux/Solaris, but I simply cannot use it on my AIX VMs since Bash isn't installed on them. I need a password that is randomly created with the following... (12 Replies)
Discussion started by: gfroute
12 Replies

2. Programming

random number

How can I choose randomly the row numbers of my file in awk? (4 Replies)
Discussion started by: Homa
4 Replies

3. Shell Programming and Scripting

Random word generation with AWK

Hi - I have a word GTTCAGAGTTCTACAGTCCGACGAT I need to extract all the possible "chunks" of 7 or above letter "words" from this. SO, my out put should be GTTCAGA TTCAGAG TCAGAGT CAGAGTTCT TCCGACGAT CAGTCCGACG etc. How can I do that with awk or any other language? I have no... (2 Replies)
Discussion started by: polsum
2 Replies

4. Programming

Random number is not reused

I need to generate and reuse a 5 digit random number every time my program is executed. But the following generates random numbers every time the function is called. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <time.h> ... (12 Replies)
Discussion started by: limmer
12 Replies

5. Shell Programming and Scripting

random number generation in ksh

i tried to use $random function in unix i simply typed print $random at shell and it returnted no value is there any function in korn shell that i can use to generate random number help is appreciated (2 Replies)
Discussion started by: er_zeeshan05
2 Replies

6. Shell Programming and Scripting

Random NUmbers Generation with out repetation

Hi I have the below code MAXCOUNT=10 count=1 echo echo "$MAXCOUNT random numbers:" echo "-----------------" while # Generate 10 ($MAXCOUNT) random integers. do number=$ + 1 ] "echo $number" let "count += 1" # Increment count. done But aftre executing this ... (8 Replies)
Discussion started by: lalitka
8 Replies

7. Shell Programming and Scripting

Regarding Random Number Genration

HI please help me in solving this issue. I have a shell script which consists of ten statemnts ( i mean 10 executable statments)...........and if i run that script the 10 statmenst will execute continously ............ But now my probelm is i have to write another script --> to... (0 Replies)
Discussion started by: lalitka
0 Replies

8. Shell Programming and Scripting

Sequence number generation on a key column

Hi Folks, Can you help me with this issue: I have to generate the numbers say from 1001 for each record in a file based on a key field, the catch is the generated number should be unique based on key column. (EMP_NUMBER) Example: Input File: EMP_NUMBER EMP_NAME 8908 ... (6 Replies)
Discussion started by: sbasetty
6 Replies

9. UNIX for Dummies Questions & Answers

Random number generation in ksh

I need to generate a random number in ksh everytime I run the script ,the range should be from 100 to 24800,I could use $RANDOM but I seem to have no control over the range of numbers ,could you please suggest some way I could implement this .Thanks. Mervin (2 Replies)
Discussion started by: mervin2006
2 Replies

10. UNIX for Dummies Questions & Answers

SQL Loader Auto Number Generation

Hi all, I have a doubt in SQL Loader. We have SEQUENCE function in SQL Loader or can create Sequence in Oracle database for generating a number sequence for a column while loading data using SQL Loader into table or multiple tables. My requirment is this. For the first run in SQL... (2 Replies)
Discussion started by: vinoth_kumar
2 Replies
Login or Register to Ask a Question