LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2012, 02:31 PM   #1
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
Question about arrays


How would you go about using an array that returns numbers 1-10 at random?
 
Old 10-14-2012, 02:45 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
In what language?
 
Old 10-14-2012, 02:48 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I'm not sure what you mean. Do you want a function that returns a random number between 1 and 10, or do you want an array full of random numbers between 1 and 10?
 
Old 10-14-2012, 05:04 PM   #4
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
I want a full array of 10 and using C++
 
Old 10-14-2012, 05:06 PM   #5
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
I want it to return them in random order like

5 8 2 1 3 9 10 7 4 6
 
Old 10-14-2012, 06:40 PM   #6
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 341

Rep: Reputation: 83
A quick Google search returned the following:

cplusplus.com
 
Old 10-14-2012, 06:46 PM   #7
croog
LQ Newbie
 
Registered: Feb 2006
Distribution: fedora-17-x86_64 Xfce
Posts: 29

Rep: Reputation: 1
If you want to return a "random" element from the array I would do something like
Code:
srand(time(0));
int myarray[10];

int getrandom(){
  return myarray[(rand()%10)];
}
 
  


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
[SOLVED] Question with c++,string and arrays? peacepanda Programming 3 10-16-2012 05:52 AM
Question about arrays in awk takayama Programming 17 05-08-2011 03:09 PM
bash arrays question introuble Programming 1 05-20-2006 04:07 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM


All times are GMT -5. The time now is 10:29 PM.

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