LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-25-2003, 10:20 PM   #1
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Rep: Reputation: 30
Getting random array input.


Language C,

say I have

string array[]={"some", "some1", "some2"};
int x=0
array[]=random(0-3);


so then, if it was say, 1, array[1] would be displayed, being some1.

(not sure if the declaration of array[] is correct, unsure of random() and unsure of array=random() )
 
Old 04-25-2003, 10:33 PM   #2
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
Also, I created a structure and I want to know if this is a legal assignment

genInv.axe=axe[rand(0-3)];

(first post show random() and this on shows rand(), I am not sure which to use so I put both :P). So basicly, 0-3 picks from 4 different positions in the array and assigns that value to genInv.axe.
 
Old 04-29-2003, 11:25 PM   #3
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
You can generate a random number in the range of 0-3 by following

int randomNumber;

/* this will generate a random number from 0 to system defined RAND_MAX */
randomNumber = rand();

/* This will limit the random number value from 0-3 */
randomNumber = randomNumber%4;

Now you can use this number as Index to your array.

outputName = array[randomNumber];

Yaser
 
  


Reply



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
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
random boot with proliant ml350 and Smart Array 642 eescar Linux - Software 1 11-17-2004 03:05 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
Random 'Input/output error' with nfs ggeorgiev Linux - Networking 0 05-26-2004 06:43 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration