LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   I need a random number generator (https://www.linuxquestions.org/questions/linux-software-2/i-need-a-random-number-generator-696021/)

newbiesforever 01-09-2009 12:30 AM

I need a random number generator
 
I'm looking for a random number generating program, but to my surprise, I didn't find one in Synaptic. My graphing calculator had that function, but I gave it away some time ago.

syg00 01-09-2009 12:41 AM

Language ? - everybody has one, even bash "echo $RANDOM"

AuroraCA 01-09-2009 01:08 AM

LInux has a built in random number generator. It may need to be initialized on your system.

See: http://linux.die.net/man/4/random

newbiesforever 01-09-2009 11:48 AM

Quote:

Originally Posted by syg00 (Post 3401967)
Language ? - everybody has one, even bash "echo $RANDOM"

That did what I need Syg00. Thanks. I guess my random number generator must be initialized.

ichrispa 01-09-2009 04:31 PM

There is a bunch of things you can use, especially on linux.

The kernels random number generator can be accessed through the device /dev/random. The pseudo-randoms are initiallized by the kernel at boottime, so they should be fairly irregular.

A couple of examples:

in bash, try
Code:

head -c 1 /dev/random | hexdump -d | gawk '{print $2}'
to display a single decade character. just typing
Code:

cat /dev/random
would display the ascii character corresponding to the given number.

You can also directly you awk to diplay randoms. Just go for
Code:

gawk '{print rand()*100}'
.

If you are into c or c++, i would suggest you read up on the manpage for rand (just type "man rand" without the quotes). For example:

Code:

#include <stdlib.h>
#include <stdio.h>

int main() {
 int i;

 //initialize the rng
 srand(time());
 i=rand()*100;

 printf("This is a random numer: %i\n", i);
 
 return 0;
}

The solution always depends on what you are trying to implement... there are more then enough ways.

newbiesforever 05-22-2009 02:04 PM

For anyone who reads this, I later found a website called random.org that has a simple random number generator.


All times are GMT -5. The time now is 06:59 PM.