Generating random values...

Need help with C, C++, perl, python, etc?

Generating random values...

Postby Xylock » 2007-04-16 15:10

:oops: I'm having trouble generating random values using gcc.

I've tried rand(), but it keeps giving me segmentation faults. :(

I did read that rand() is not advised in a multi-threading environment, and also somewhere else that the linux kernel could generate random numbers.

Anyone able to give me some advise on how to achieve this? This has always stumped me in C :P I dont really want to have to write my own (copy someone elses) generator, lol, I'd rather use an existing one :D
Using rm -rvf * to remove old backups... lazy.
Realising you were in / as root ... priceless.
Posts: 43
Joined: 2007-04-11 13:28

Postby sinical » 2007-04-17 02:27

Code: Select all
/bin/bash echo $RANDOM

How are you calling it in C though.. I know that something along the lines of

Code: Select all
x = rand()  %  y;

While it does compile no errors will cause issues
Every cloud has a silver lining, except for the mushroom shaped ones, which have a lining of Strontium 90.
umop apisdn
User avatar
Posts: 1022
Joined: 2007-03-25 11:52

Postby Optional » 2007-04-17 07:06

Make sure you're seeding the random number generator by calling srand() first.

User avatar
Posts: 349
Joined: 2007-02-05 05:02

Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests