LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-11-2007, 07:39 AM   #1
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Rep: Reputation: 15
randomize non repeated numbers


Hello,
I'm still newbie linux user, can someone help me to whrite a script that could generate randomly N numbers that are not repeated within range [1..X]

thanx
 
Old 06-11-2007, 08:20 AM   #2
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
ok, I'll help you:
pick a language.
learn how to create and use arrays or lists or dictionaries.
read
Code:
man urandom
use that to generate the random number. write it into the array and make sure that array is checked before you write in it again, to avoid duplicates.
let me know how you're doing...
[EDIT]
WAIT A MINUTE...
I just had a look at your homepage, seeing all your certificates, practical experience, dimplomas and all.
THIS CAN'T BE REAL.
something is extremely rotten in the state of LQ.
Are you a fake or just extremely lazy?
You'll be on my ignore list from now on.
[/EDIT]

Last edited by baikonur; 06-11-2007 at 01:17 PM.
 
Old 06-11-2007, 01:26 PM   #3
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
I'm still not over it.
this is one especially fascinating entry on his hp:

Quote:
Skills Profile:



§ Programming Languages:

Borland Delphi, SQL, C , C++, Pascal ,Lisp. Excellent

JBuilder , C++Builder , java, HTML, Quick Basic, Matlab

Prolog , Lisp, Assembly , ADA , Flash. Good
@mystical dervish: i reconsidered. i won't ignore you.
i'll put you on my buddy list and whenever i find you asking about simple programming tasks again i'll warn everyone!!
 
Old 06-12-2007, 06:57 AM   #4
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by baikonur
I'm still not over it.
this is one especially fascinating entry on his hp:



@mystical dervish: i reconsidered. i won't ignore you.
i'll put you on my buddy list and whenever i find you asking about simple programming tasks again i'll warn everyone!!
Ok, but you have to verify before you decide to adjudge the others,it's very good that you changed to be right. if you don't believe someone that doesnt mean that he is fake. As well I mentioned in my post that Im new user with linux, just 65 days ago with linux, so I'm not lasy dear, one who works with "non repeated numbers" may be engaged in some real technical matter, I think you know that..

if someone wants verify my java cert(for example) he may ask me for that, or for any technical question about my experience
anyway that's good that you returned to th right, but be carful that you can't injure the others by your stones

thanx
 
Old 06-12-2007, 07:07 AM   #5
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
i went through some of your other threads and it's always the same: you expect people to do your homework. many people around here do that, but they don't have a CV like you do. i'm sorry, s.o. who claims to be excellent at lisp and c won't ask questions like that.
so i'll stick with it: you are either abusing the community because you are too lazy to read for yourself (which is not acceptable from some one with a computer background like yours) or you're a fake.
 
Old 06-12-2007, 11:22 AM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Well, I'm not going to judge. Besides, these kinds of problems are fun. Here's a solution in MzScheme:
Code:
(define internal (lambda (l max size)
                   (if (= (length l) size)
                       l
                       (let ((rnd (+ (random max) 1)))
                         (if (member rnd l)
                             (internal l max size)
                             (internal (cons rnd l) max size))))))

(define non-repeating-random (lambda (max size)
                               (if (> size max)
                                   (error "no such list")
                                   (internal '() max size))))
Call it like so:
Code:
Welcome to DrScheme, version 370 [3m].
Language: Pretty Big (includes MrEd and Advanced Student) custom.
> (non-repeating-random 5 3)
(list 1 2 4)
> (non-repeating-random 3 5)
no such list

Last edited by taylor_venable; 06-12-2007 at 11:24 AM.
 
Old 06-13-2007, 01:48 AM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by mystical dervish
Hello,
I'm still newbie linux user, can someone help me to whrite a script that could generate randomly N numbers that are not repeated within range [1..X]

This bash script should do what you want:

Code:
bot=1 num=${2:?}
eval "set -- {1..${1:?}"
a=( "$@" )
eval "set -- {1..$num}"
for n
do
    r=$(( $RANDOM % ${#a[@]} ))
    printf "%d\n" "${a[$r]}"
    unset a[$r]
    a=( "${a[@]}" )
done
Call it with X and N (in that order) as arguments.

 
Old 06-13-2007, 03:42 AM   #8
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by taylor_venable
Well, I'm not going to judge. Besides, these kinds of problems are fun. Here's a solution in MzScheme:
Code:
(define internal (lambda (l max size)
                   (if (= (length l) size)
                       l
                       (let ((rnd (+ (random max) 1)))
                         (if (member rnd l)
                             (internal l max size)
                             (internal (cons rnd l) max size))))))

(define non-repeating-random (lambda (max size)
                               (if (> size max)
                                   (error "no such list")
                                   (internal '() max size))))
Call it like so:
Code:
Welcome to DrScheme, version 370 [3m].
Language: Pretty Big (includes MrEd and Advanced Student) custom.
> (non-repeating-random 5 3)
(list 1 2 4)
> (non-repeating-random 3 5)
no such list
I would like thank you for this lisp code, but now I don't use a list compiler for my project
 
Old 06-13-2007, 03:43 AM   #9
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cfaj

This bash script should do what you want:

Code:
bot=1 num=${2:?}
eval "set -- {1..${1:?}"
a=( "$@" )
eval "set -- {1..$num}"
for n
do
    r=$(( $RANDOM % ${#a[@]} ))
    printf "%d\n" "${a[$r]}"
    unset a[$r]
    a=( "${a[@]}" )
done
Call it with X and N (in that order) as arguments.

Thanx a lot
 
Old 06-13-2007, 03:59 AM   #10
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by baikonur
i went through some of your other threads and it's always the same: you expect people to do your homework. many people around here do that, but they don't have a CV like you do. i'm sorry, s.o. who claims to be excellent at lisp and c won't ask questions like that.
so i'll stick with it: you are either abusing the community because you are too lazy to read for yourself (which is not acceptable from some one with a computer background like yours) or you're a fake.
it's the second time you tried to judge, and abuse me, you should respect the new users to present a good reputation about the community with whome you work. I'm not going to do a homework, I read and learn myself in a way that I see it suitable for me, as you know there are different ways to learn, one of them is by examples, if you see that you are professional don't reply the simple questions, but you have to know that noone is professional in everything.
it's enough for now , but you should apologize for your false judgement
 
Old 06-15-2007, 04:34 AM   #11
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Thumbs down

Quote:
I'm not going to do a homework,
exactly, you expect others to do it for you.
you are obviously a programmer who could easily solve his little problems on his own, but you prefer to have others do it. if people want to do that for you - well, that ain't my business. i for one truly dislike people who abuse others like that. you state that your linux knowledge is not broad enough and i doubt that, but just in case - i can help.

man -k [topic]
man [file|program]
"/" to search
"n" to go to the next hit

that and a internet search engine is all you need.
of course, you don't need it. you simply need a bunch of nice guys to to stuff for you. unfortunately, you seem to get it here.

i certainly won't apologize to you.
guys like you annoy me a lot. you obviously know much much more than i do about programming, but i would at least do the following things b4 i post a question:

1. try on my own
2. ask questions the way it's supposed to be
2a) tell the guy that bothers to read my question what i've tried already
2b) tell him where i'm stuck

as opposed to:

Quote:
I'm still newbie linux user, can someone help me to whrite a script that could generate randomly N numbers that are not repeated within range [1..X]
 
  


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
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM
Randomize /etc/motd? lengau Linux - General 4 09-05-2005 11:01 PM
How to randomize Splash Image using Gnome??? thinhla Linux - General 1 07-15-2005 10:39 AM
Repeated seg faults Tick Linux - General 5 07-29-2004 07:09 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

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

All times are GMT -5. The time now is 08:51 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
Open Source Consulting | Domain Registration