LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-15-2002, 01:19 PM   #1
J-Stew
LQ Newbie
 
Registered: Aug 2002
Location: North Georgia Mountains :-)
Distribution: RedHat7.3
Posts: 12

Rep: Reputation: 0
Generating random numbers for C program


i have an arbitrary number called 'result'

i need for result to be equal to a random that can range anywhere from 1 to 100...

does anyone have any idea how to do this?

i have searched the internet for an answer and have not found one yet... any help would be appreciated....
 
Old 08-15-2002, 01:24 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,373

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
erm, something wrong with the random()/rand() functions?? check the manpage

Last edited by acid_kewpie; 08-15-2002 at 01:29 PM.
 
Old 08-15-2002, 01:33 PM   #3
J-Stew
LQ Newbie
 
Registered: Aug 2002
Location: North Georgia Mountains :-)
Distribution: RedHat7.3
Posts: 12

Original Poster
Rep: Reputation: 0
err next...

err ... next
 
Old 08-15-2002, 01:34 PM   #4
J-Stew
LQ Newbie
 
Registered: Aug 2002
Location: North Georgia Mountains :-)
Distribution: RedHat7.3
Posts: 12

Original Poster
Rep: Reputation: 0
i tried this:

result = random(100);

like it said but i kept getting errors
 
Old 08-15-2002, 04:59 PM   #5
warhorse
LQ Newbie
 
Registered: Aug 2002
Location: Pittsburgh
Distribution: Debian
Posts: 24

Rep: Reputation: 15
int rand(void)
returns a pseudo-random integer in the range 0 to RAND_MAX which is at least 32767

void srand(unsigned int seed)
srand uses seed as the seed for a new sequence of pseudo random numbers. The initial seed is 1.

The above is from page 252 of
The C Programming Language by Kernighan and Ritchie. A very highly recommended book.
 
Old 08-15-2002, 05:45 PM   #6
gdrobson
Member
 
Registered: Jun 2002
Location: Ontario, Canada
Distribution: RH8.0
Posts: 65

Rep: Reputation: 15
Here you go boss:

#include <stdlib.h>
#include <time.h>

int RandNumGen(){
int x = rand() % 101;
return x;
}

int main(){
int x;
srand(time(NULL));
x = RandNumGen();
printf ("\n\n%i\n\n", x);
return 0;
}
 
Old 08-19-2002, 05:46 PM   #7
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
If you use: int x = rand() % 101;
you get 0 <= x <= 100

Try: int x = (rand() % 100) + 1;
you get 1 <= x <= 100
 
Old 08-20-2002, 11:14 AM   #8
Sfin
Member
 
Registered: Apr 2002
Location: Michigan
Distribution: Slackware 8.0
Posts: 197

Rep: Reputation: 30
to get a random number between 1 to 100 just use:

result = 1 + rand() % 100;


rand() --> Gives you a number between 0 and RAND_MAX
rand() % N --> Gives you a number between 0 and (N - 1)
M + rand() % N --> This shifts the above statement M numbers, so you get a number between M and (N - 1) + M


Hope this clearifies things a little.
 
Old 08-20-2002, 08:14 PM   #9
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
rand() tends to give you crappy random numbers. a better way is to read from /dev/urandom just open the file and read a character at a time then typecast it as a int and mod it by 100.
 
Old 08-21-2002, 03:15 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,373

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
Re: err next...

Quote:
Originally posted by J-Stew
err ... next
?? fine... whatever.
 
Old 02-05-2004, 08:19 AM   #11
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
These are some of the best suggestions i've seen so far for creating a RNG but...

If you compile this:

Code:
#include <stdlib.h>
#include <time.h>
                                                                                                       
int main() {
  int x;

  srand(time(NULL));

  x = 1 + rand() % 100;
  printf("%d\n", x);
  return 0;
and then run it, u get the same number if it's run twice (or more) in the same second (roughly)... K thats probably me being fussy but if ya need something to continuously chuck you random numbers for 5 seconds you're screwed

There must be a better way than using time(), hmmm i'm gonna sleep on it lol
 
Old 02-05-2004, 08:56 AM   #12
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
So use something that get you millisecond values. Such as the ftime function found in sys/timeb.h. It gives you a timeb structure, which gives you a time_t time, and a millitm, which is the number of seconds since the time_t, so you need to add them something like so:

Code:
#include <sys/timeb.h>

int main()
{
   unsigned int ms;
   struct timeb t;

   ftime(&t);

   ms = t.time * 1000 + t.millitm;   
   return 0;
}
Note: I'm sure there are probably better functions for getting millisecond values in Linux, but I'm still not all that familiar with all the standard libraries generally available with Linux. In Windows, I would use the API function timeGetTime(). Anyone know of a similar method for Linux?
 
Old 02-05-2004, 09:00 PM   #13
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
Yeah that works really well - but when ya run it you get long negative numbers, what about short positive numbers?

Hmmmm there must be a way
 
Old 02-05-2004, 10:22 PM   #14
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 31
you can also read in from /dev/urandom (or /dev/random but that requires user input)
 
Old 02-06-2004, 04:49 AM   #15
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
rand() is a crappy PRNG?

It depends on what you're doing with it. For encryption it's out of the question. For number guessing games like 'pick a number from 1 to 20'
it's fine. As several posters pointed out, if you call srand() with a seed value that changes (like some kind of fine-grained time value) you avoid the problem of the sequence of numbers being the same.

Linux provides lrand48 (man drand48) that is as easy to use as rand() and is considered a much better PRNG - depending on what you are doing.

For most of what beginning programmers do rand() is just fine.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
generating random numbers in C abk4523 Programming 20 01-10-2011 04:08 PM
Random numbers in C loke137 Programming 9 09-11-2010 10:37 AM
code for generating random samples! aru_04 Programming 2 07-21-2005 08:35 AM
Generating Gaussian Random Numbers R00ts Programming 2 08-10-2004 11:51 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM


All times are GMT -5. The time now is 12:17 PM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration