how to create random no between 10 to 40 in C


 
Thread Tools Search this Thread
Top Forums Programming how to create random no between 10 to 40 in C
# 1  
Old 07-21-2007
how to create random no between 10 to 40 in C

can any one tell me how to create integer random no between 10 to 40 in C language.When i m using random() or rand() functions they r creting some
long int which is not required
# 2  
Old 07-21-2007
Divide the random number by 31 ( 10 through 40) take the remainder and add 10.
Create an array of 31 entries, run 1,000,000 iterations and count how many times each number appears to see if you have 25000 of each number, or what the deviation is.
# 3  
Old 07-23-2007
The below example shows a simple solution of your problem.

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

int main ()
{
  /* initialize random generator */
  srand ( time(NULL) );

  /* generate random numbers */ 	
  printf ("A number between 10 and 40: %d\n", (rand() % 30 + 10) );

  return 0;
}

Best regards,
Iliyan Varshilov

Last edited by ilko_partizan; 07-23-2007 at 10:04 AM..
# 4  
Old 08-08-2007
one query..in the above code

Why the resule is being divided by 30 to get the result & added to 10.

How to reach the above decision.
# 5  
Old 08-08-2007
Quote:
Originally Posted by bishweshwar
Why the resule is being divided by 30 to get the result & added to 10.

How to reach the above decision.
Because a random number between 10 and 40 was requested - the original result (which would be between 0 and 30) is just shifted by adding 10.
Cheers
ZB

Last edited by zazzybob; 08-08-2007 at 08:31 AM..
# 6  
Old 08-19-2007
hi
Quote:
Originally Posted by useless79
can any one tell me how to create integer random no between 10 to 40 in C language.
Quote:
Originally Posted by jgt
Divide the random number by 31 ( 10 through 40) take the remainder and add 10.
Quote:
Originally Posted by ilko_partizan
Code:
  printf ("A number between 10 and 40: %d\n", (rand() % 30 + 10) );

this is not the way you should create a random number between 30 and 40.
a correct way is

Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
    printf("%d\n",30+(int)((rand()/(RAND_MAX +1.0))*11));
return 0;
}

you should not use the % operator to constract a random number from the last digits of a a random number generator. most random generators are neither contructed that their last digits are random nor are they testet against this assumption. They are constructed and tested that the leading digits are random.

rand() gives a random number bertween 0 and RAND_MAX

rand()/(RAND_MAX +1.0) gives a float random number greater or equal 0 and less then 1

(rand()/(RAND_MAX +1.0))*11 gives a random number greater or equal 0 and less then 11

(int)((rand()/(RAND_MAX +1.0))*11) gives a random number from the set {0,...,10}

finally 30+(int)((rand()/(RAND_MAX +1.0))*11) gives a random number from the set {30,...,40}

information on random generators and further links can be found at
USING THE C OR C++ rand() FUNCTION
mfg guenter
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create 'n' number random pairwise combination of words

File 1 contains the list of words that needed to be randomly paired: Tiger Cat Fish Frog Dog Mouse Elephant Monkey File 2 contains the pairs that should not be used (in any solution) during random pairing. Elephant-Dog Cat-Fish Monkey-Frog Dog-Elephant, Fish-Cat, Frog-Monkey... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. Shell Programming and Scripting

Create random number

Hi, I'm trying to create a script that will print random numbers with length of three. Below is the expected out. 928-377-899 942-458-310 951-948-511 962-681-415 995-161-708 997-997-209 thanks (4 Replies)
Discussion started by: reignangel2003
4 Replies

5. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. Solaris

Solaris Boot Problems, random messages [/etc/rcS: /etc/dfs/sharetab: cannot create]

Hello All, I have all of a sudden developed issues with booting up one of my Solaris Servers. Upon a routine reboot, I was faced with the following errors: Feb 1 07:56:44 sco1-au-tci scsi: WARNING: /pci@1c,600000/scsi@2/sd@0,0 (sd0): Feb 1 07:56:44 sco1-au-tci Error for Command: read(10)... (9 Replies)
Discussion started by: ranjtech
9 Replies

8. Shell Programming and Scripting

Random

My problem is as follow and i hope you can help: I currently have this function: stored_word() { number=$RANDOM let "number %= 21" case $number in 0 ) echo "energy" ;; 1 ) echo "touch" ;; 2 ) echo "climbing" ;; 3 ) echo "declare" ;; 4 ) echo "marry" ;; 5 ) echo "relax" ... (8 Replies)
Discussion started by: keyvan
8 Replies

9. Shell Programming and Scripting

create a Random passwd

Hello Everyone, I am wondering how to create a Random Password of 8 characters for a user. It has to be run in a script. Also, The user have to be asked to change his/her password the first time they login. I tried using genPass but seems like my system doesn't support that command. For... (1 Reply)
Discussion started by: bashirpopal
1 Replies

10. UNIX Desktop Questions & Answers

How do I create desktop icons for the shell programs I create???

I am a bash shell programmer and I create programs on occasional basis. Now, I dont want my programs to be run by typing out its name at a command line. I want to make it as user friendly as possible. I want to create icons on the desktop so users can click on it. mind you, I said "desktop... (7 Replies)
Discussion started by: TRUEST
7 Replies
Login or Register to Ask a Question